Skip to content

Instantly share code, notes, and snippets.

@ypchen520
Last active November 26, 2023 04:28
Show Gist options
  • Select an option

  • Save ypchen520/a4c1ce92c6074c64afc1ba82fe02e1a8 to your computer and use it in GitHub Desktop.

Select an option

Save ypchen520/a4c1ce92c6074c64afc1ba82fe02e1a8 to your computer and use it in GitHub Desktop.

__main__.py

mypackage/
β”œβ”€β”€ __init__.py
└── __main__.py
  • This file is used to define the entry point of a Python package.
  • When a directory is used as a package (contains an __init__.py file), and it is executed as the main program (using python -m <package>), the __main__.py file in that package is executed.

main.py

  • This file is often used as the entry point for a standalone Python script or a simple program.
  • The if __name__ == "__main__": block ensures that the main() function is only called if the script is executed directly and not imported as a module elsewhere.
# main.py
def main():
    print("This is the main function.")

if __name__ == "__main__":
    main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment