Skip to content

Instantly share code, notes, and snippets.

@SeanSyue
Created August 12, 2018 03:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save SeanSyue/ff57bc2afe988d65af0330e3dc3b192e to your computer and use it in GitHub Desktop.
Example on understanding "__name__ == '__main__'" in Python.
print("This will always be shown first\n")
def main():
print("Calling first_module's main method\n")
if __name__ == '__main__':
# the following will be shown only in `first_module.py`
main()
print("== Run directly ==\n")
else:
# the following will be shown only in `first_module.py`
print("== Run from import ==\n")
print("Name of the first_module: {}\n".format(__name__))
import first_module
# calling the `main()` function of `first_module`
first_module.main()
# this will only be shown in `second_module.py`
print("Name of the second_module: {}".format(__name__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment