Python - Main (Entry Point)

> Procedural Languages > Python > Python - (Shipping|Packaging)

1 - About

Advertising

3 - Specification

4 - Type

4.1 - Package

4.1.1 - init

def main():
    """Entry point for the application script"""
    print("Call your main application code here")

4.1.2 - executable scripts

To provide executable scripts, use entry points in preference to the “scripts” keyword. Entry points provide cross-platform support and allow pip to create the appropriate form of executable for the target platform.

For example, the following would provide a command called sample which executes the function main from this package when invoked:

setup.py
entry_points={  # Optional
        'console_scripts': [
            'sample=sample:main',
        ],
    },

4.2 - Script

When you want to make a script executable, the if statement makes that The code is not executed when the script is imported as a module.

foo.py
import sys
 
def main(argv):
    # my code here
 
if __name__ == "__main__":
    main(sys.argv)

where __name__ has the value:

  • foo when the module is imported
  • or __main__ when executed from Python.
Advertising

5 - Documentation / Reference