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__.pyfile), and it is executed as the main program (usingpython -m <package>), the__main__.pyfile in that package is executed.
- 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 themain()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()