What does the
if __name__ == "__main__":
do?
To outline the basics:
The global variable, __name__
, in the module that is the entry point to your program, is '__main__'
. Otherwise, it's the name you import the module by.
So, code under the if
block will only run if the module is the entry point to your program.
It allows the code in the module to be importable by other modules, without executing the code block beneath on import.
Why do we need this?
Say you're writing a Python script designed to be used as a module:
def do_important():
"""This function does something very important"""
You could test the module by adding this call of the function to the bottom:
do_important()
and running it (on a command prompt) with something like:
~$ python important.py
However, if you want to import the module to another script:
import important
On import, the do_important
function would be called, so you'd probably comment out your function call, do_important()
, at the bottom.
# do_important() # I must remember to uncomment to execute this!
And then you'll have to remember whether or not you've commented out your test function call. And this extra complexity would mean you're likely to forget, making your development process more troublesome.
The __name__
variable points to the namespace wherever the Python interpreter happens to be at the moment.
Inside an imported module, it's the name of that module.
But inside the primary module (or an interactive Python session, i.e. the interpreter's Read, Eval, Print Loop, or REPL) you are running everything from its "__main__"
.
So if you check before executing:
if __name__ == "__main__":
do_important()
With the above, your code will only execute when you're running it as the primary module (or intentionally call it from another script).
There's a Pythonic way to improve on this, though.
What if we want to run this business process from outside the module?
If we put the code we want to exercise as we develop and test in a function like this and then do our check for '__main__'
immediately after:
def main():
"""business logic for when running this module as the primary one!"""
setup()
foo = do_important()
bar = do_even_more_important(foo)
for baz in bar:
do_super_important(baz)
teardown()
# Here's our payoff idiom!
if __name__ == '__main__':
main()
We now have a final function for the end of our module that will run if we run the module as the primary module.
It will allow the module and its functions and classes to be imported into other scripts without running the main
function, and will also allow the module (and its functions and classes) to be called when running from a different '__main__'
module, i.e.
import important
important.main()
This idiom can also be found in the Python documentation in an explanation of the __main__
module. That text states:
This module represents the (otherwise anonymous) scope in which the interpreter’s main program executes — commands read either from standard input, from a script file, or from an interactive prompt. It is this environment in which the idiomatic “conditional script” stanza causes a script to run:
if __name__ == '__main__': main()