What does this do, and why should one include the if
statement?
if __name__ == "__main__":
print("Hello, World!")
If you are trying to close a question where someone should be using this idiom and isn't, consider closing as a duplicate of Why is Python running my module when I import it, and how do I stop it? instead. For questions where someone simply hasn't called any functions, or incorrectly expects a function named main
to be used as an entry point automatically, use Why doesn't the main() function run when I start a Python script? Where does the script start running?.
Best Answer
Short Answer
It's boilerplate code that protects users from accidentally invoking the script when they didn't intend to. Here are some common problems when the guard is omitted from a script:
If you import the guardless script in another script (e.g.
import my_script_without_a_name_eq_main_guard
), then the latter script will trigger the former to run at import time and using the second script's command line arguments. This is almost always a mistake.If you have a custom class in the guardless script and save it to a pickle file, then unpickling it in another script will trigger an import of the guardless script, with the same problems outlined in the previous bullet.
Long Answer
To better understand why and how this matters, we need to take a step back to understand how Python initializes scripts and how this interacts with its module import mechanism.
Whenever the Python interpreter reads a source file, it does two things:
it sets a few special variables like
__name__
, and thenit executes all of the code found in the file.
Let's see how this works and how it relates to your question about the
__name__
checks we always see in Python scripts.Code Sample
Let's use a slightly different code sample to explore how imports and scripts work. Suppose the following is in a file called
foo.py
.Special Variables
When the Python interpreter reads a source file, it first defines a few special variables. In this case, we care about the
__name__
variable.When Your Module Is the Main Program
If you are running your module (the source file) as the main program, e.g.
the interpreter will assign the hard-coded string
"__main__"
to the__name__
variable, i.e.When Your Module Is Imported By Another
On the other hand, suppose some other module is the main program and it imports your module. This means there's a statement like this in the main program, or in some other module the main program imports:
The interpreter will search for your
foo.py
file (along with searching for a few other variants), and prior to executing that module, it will assign the name"foo"
from the import statement to the__name__
variable, i.e.Executing the Module's Code
After the special variables are set up, the interpreter executes all the code in the module, one statement at a time. You may want to open another window on the side with the code sample so you can follow along with this explanation.
Always
It prints the string
"before import"
(without quotes).It loads the
math
module and assigns it to a variable calledmath
. This is equivalent to replacingimport math
with the following (note that__import__
is a low-level function in Python that takes a string and triggers the actual import):It prints the string
"before function_a"
.It executes the
def
block, creating a function object, then assigning that function object to a variable calledfunction_a
.It prints the string
"before function_b"
.It executes the second
def
block, creating another function object, then assigning it to a variable calledfunction_b
.It prints the string
"before __name__ guard"
.Only When Your Module Is the Main Program
__name__
was indeed set to"__main__"
and it calls the two functions, printing the strings"Function A"
and"Function B 10.0"
.Only When Your Module Is Imported by Another
__name__
will be"foo"
, not"__main__"
, and it'll skip the body of theif
statement.Always
"after __name__ guard"
in both situations.Summary
In summary, here's what'd be printed in the two cases:
Why Does It Work This Way?
You might naturally wonder why anybody would want this. Well, sometimes you want to write a
.py
file that can be both used by other programs and/or modules as a module, and can also be run as the main program itself. Examples:Your module is a library, but you want to have a script mode where it runs some unit tests or a demo.
Your module is only used as a main program, but it has some unit tests, and the testing framework works by importing
.py
files like your script and running special test functions. You don't want it to try running the script just because it's importing the module.Your module is mostly used as a main program, but it also provides a programmer-friendly API for advanced users.
Beyond those examples, it's elegant that running a script in Python is just setting up a few magic variables and importing the script. "Running" the script is a side effect of importing the script's module.
Food for Thought
Question: Can I have multiple
__name__
checking blocks? Answer: it's strange to do so, but the language won't stop you.Suppose the following is in
foo2.py
. What happens if you saypython foo2.py
on the command-line? Why?foo3.py
(having removed the__name__
check):