Are things above __name__=='__main__' executed? - python

I had the impression that the code only below name='main' is executed if you run a module directly. However, I saw a line mr = MapReduct.MapReduce() appearing above that statement? How is that executed and what is the reason one would put it above the if clause?
import MapReduce
import sys
"""
Word Count Example in the Simple Python MapReduce Framework
"""
mr = MapReduce.MapReduce()
if __name__ == '__main__':
inputdata = open(sys.argv[1])
mr.execute(inputdata, mapper, reducer)

Of course, all the code before if __name__ == '__main__': gets executed. A script is processed from top to bottom, and each expression or statement that's found is executed in order. But the if __name__ == '__main__': line is special: it'll get run if the script is called from the command line.
By convention, the if __name__ == '__main__': line is put at the end, to make sure that all the code that it depends on has been evaluated up to this point.
Take a look at this other question to understand what's happening in that line.

Everything in the file is executed, but most of what you put above __name__ == '__main__' is just function or class definitions - executing those just define a function or class and don't produce any noticeable side effects.
If you put a print statement in the file outside of those definitions for example, you'll see some output.

Yes, The entire script is executed.
The reason we have a main guard is to enable your program to be used both as a standalone script ( in which case the __name__ variable is defined as "__main__") or imported as a module from another python script. In this case, we don't want any code to be run, we just want definitions of the code to be available to whoever loaded us.
One typical use of the __main__ guard is to run unit tests if the python file is run on its own. For example let us imagine we have file mymodule.py:
def my_function(foo):
"""This function will add 54 to foo.
The following two lines are 'doctests' and can be run
automatically to show that the module's function work as expected.
>>> my_function(6)
60
"""
return foo + 54
if __name__ == "__main__":
import doctest
doctest.testmod() # this will run all doctests in this file
If we now run this on the command line, the tests will be run to check that the module works as expected.
Now, from another file, or from the command line, we can import mymodule.py:
>>> import mymodule # Note that the tests will not be run when importing
>>> mymodule.my_function(3)
57
And the tests will not run because the __name__ variable will not be __main__ when the script is imported.
You can test this with a simple file (test.py):
➤ cat test.py
#!/usr/bin/env python
print("my name is: ", __name__)
➤ python test.py
my name is: __main__
➤ python
Python 3.3.1 (default, Apr 6 2013, 19:11:39)
[GCC 4.8.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
my name is: test
>>>

Related

I'm trying out pygame and the screen won't stay open [duplicate]

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?.
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 then
it 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.
# Suppose this is foo.py.
print("before import")
import math
print("before function_a")
def function_a():
print("Function A")
print("before function_b")
def function_b():
print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
print("after __name__ guard")
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.
python foo.py
the interpreter will assign the hard-coded string "__main__" to the __name__ variable, i.e.
# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__"
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:
# Suppose this is in some other main program.
import foo
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.
# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"
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 called math. This is equivalent to replacing import math with the following (note that __import__ is a low-level function in Python that takes a string and triggers the actual import):
# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
It prints the string "before function_a".
It executes the def block, creating a function object, then assigning that function object to a variable called function_a.
It prints the string "before function_b".
It executes the second def block, creating another function object, then assigning it to a variable called function_b.
It prints the string "before __name__ guard".
Only When Your Module Is the Main Program
If your module is the main program, then it will see that __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
(instead) If your module is not the main program but was imported by another one, then __name__ will be "foo", not "__main__", and it'll skip the body of the if statement.
Always
It will print the string "after __name__ guard" in both situations.
Summary
In summary, here's what'd be printed in the two cases:
# What gets printed if foo is the main program
before import
before function_a
before function_b
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before function_a
before function_b
before __name__ guard
after __name__ guard
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 say python foo2.py on the command-line? Why?
# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo2 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
if __name__ == "__main__":
print("m1")
function_a()
print("m2")
print("t2")
Now, figure out what will happen if you remove the __name__ check in foo3.py:
# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo3 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
print("m1")
function_a()
print("m2")
print("t2")
What will this do when used as a script? When imported as a module?
# Suppose this is in foo4.py
__name__ = "__main__"
def bar():
print("bar")
print("before __name__ guard")
if __name__ == "__main__":
bar()
print("after __name__ guard")
When your script is run by passing it as a command to the Python interpreter,
python myscript.py
all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets run. Unlike other languages, there's no main() function that gets run automatically - the main() function is implicitly all the code at the top level.
In this case, the top-level code is an if block. __name__ is a built-in variable which evaluates to the name of the current module. However, if a module is being run directly (as in myscript.py above), then __name__ instead is set to the string "__main__". Thus, you can test whether your script is being run directly or being imported by something else by testing
if __name__ == "__main__":
...
If your script is being imported into another module, its various function and class definitions will be imported and its top-level code will be executed, but the code in the then-body of the if clause above won't get run as the condition is not met. As a basic example, consider the following two scripts:
# file one.py
def func():
print("func() in one.py")
print("top-level in one.py")
if __name__ == "__main__":
print("one.py is being run directly")
else:
print("one.py is being imported into another module")
# file two.py
import one
print("top-level in two.py")
one.func()
if __name__ == "__main__":
print("two.py is being run directly")
else:
print("two.py is being imported into another module")
Now, if you invoke the interpreter as
python one.py
The output will be
top-level in one.py
one.py is being run directly
If you run two.py instead:
python two.py
You get
top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly
Thus, when module one gets loaded, its __name__ equals "one" instead of "__main__".
Create the following two files:
# a.py
import b
# b.py
print("__name__ equals " + __name__)
if __name__ == '__main__':
print("if-statement was executed")
Now run each file individually.
Running python a.py:
$ python a.py
__name__ equals b
When a.py is executed, it imports the module b. This causes all the code inside b to run. Python sets globals()['__name__'] in the b module to the module's name, b.
Running python b.py:
$ python b.py
__name__ equals __main__
if-statement was executed
When only the file b.py is executed, Python sets globals()['__name__'] in this file to "__main__". Therefore, the if statement evaluates to True this time.
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?
Developing and Testing Your Code
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
The Problem
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.
A Better Way
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).
An Even Better Way
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()
if __name__ == "__main__" is the part that runs when the script is run from (say) the command line using a command like python myscript.py.
What does if __name__ == "__main__": do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
at the end of your mycode.py script,
when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
There are lots of different takes here on the mechanics of the code in question, the "How", but for me none of it made sense until I understood the "Why". This should be especially helpful for new programmers.
Take file "ab.py":
def a():
print('A function in ab file');
a()
And a second file "xy.py":
import ab
def main():
print('main function: this is where the action is')
def x():
print ('peripheral task: might be useful in other projects')
x()
if __name__ == "__main__":
main()
What is this code actually doing?
When you execute xy.py, you import ab. The import statement runs the module immediately on import, so ab's operations get executed before the remainder of xy's. Once finished with ab, it continues with xy.
The interpreter keeps track of which scripts are running with __name__. When you run a script - no matter what you've named it - the interpreter calls it "__main__", making it the master or 'home' script that gets returned to after running an external script.
Any other script that's called from this "__main__" script is assigned its filename as its __name__ (e.g., __name__ == "ab.py"). Hence, the line if __name__ == "__main__": is the interpreter's test to determine if it's interpreting/parsing the 'home' script that was initially executed, or if it's temporarily peeking into another (external) script. This gives the programmer flexibility to have the script behave differently if it's executed directly vs. called externally.
Let's step through the above code to understand what's happening, focusing first on the unindented lines and the order they appear in the scripts. Remember that function - or def - blocks don't do anything by themselves until they're called. What the interpreter might say if mumbled to itself:
Open xy.py as the 'home' file; call it "__main__" in the __name__ variable.
Import and open file with the __name__ == "ab.py".
Oh, a function. I'll remember that.
Ok, function a(); I just learned that. Printing 'A function in ab file'.
End of file; back to "__main__"!
Oh, a function. I'll remember that.
Another one.
Function x(); ok, printing 'peripheral task: might be useful in other projects'.
What's this? An if statement. Well, the condition has been met (the variable __name__ has been set to "__main__"), so I'll enter the main() function and print 'main function: this is where the action is'.
The bottom two lines mean: "If this is the "__main__" or 'home' script, execute the function called main()". That's why you'll see a def main(): block up top, which contains the main flow of the script's functionality.
Why implement this?
Remember what I said earlier about import statements? When you import a module it doesn't just 'recognize' it and wait for further instructions - it actually runs all the executable operations contained within the script. So, putting the meat of your script into the main() function effectively quarantines it, putting it in isolation so that it won't immediately run when imported by another script.
Again, there will be exceptions, but common practice is that main() doesn't usually get called externally. So you may be wondering one more thing: if we're not calling main(), why are we calling the script at all? It's because many people structure their scripts with standalone functions that are built to be run independent of the rest of the code in the file. They're then later called somewhere else in the body of the script. Which brings me to this:
But the code works without it
Yes, that's right. These separate functions can be called from an in-line script that's not contained inside a main() function. If you're accustomed (as I am, in my early learning stages of programming) to building in-line scripts that do exactly what you need, and you'll try to figure it out again if you ever need that operation again ... well, you're not used to this kind of internal structure to your code, because it's more complicated to build and it's not as intuitive to read.
But that's a script that probably can't have its functions called externally, because if it did it would immediately start calculating and assigning variables. And chances are if you're trying to re-use a function, your new script is related closely enough to the old one that there will be conflicting variables.
In splitting out independent functions, you gain the ability to re-use your previous work by calling them into another script. For example, "example.py" might import "xy.py" and call x(), making use of the 'x' function from "xy.py". (Maybe it's capitalizing the third word of a given text string; creating a NumPy array from a list of numbers and squaring them; or detrending a 3D surface. The possibilities are limitless.)
(As an aside, this question contains an answer by #kindall that finally helped me to understand - the why, not the how. Unfortunately it's been marked as a duplicate of this one, which I think is a mistake.)
The code under if __name__ == '__main__': will only be executed if the module is invoked as a script.
As an example, consider the following module my_test_module.py:
# my_test_module.py
print('This is going to be printed out, no matter what')
if __name__ == '__main__':
print('This is going to be printed out, only if user invokes the module as a script')
First possibility: Import my_test_module.py in another module
# main.py
import my_test_module
if __name__ == '__main__':
print('Hello from main.py')
Now if you invoke main.py:
python main.py
>> 'This is going to be printed out, no matter what'
>> 'Hello from main.py'
Note that only the top-level print() statement in my_test_module is executed.
Second possibility: Invoke my_test_module.py as a script
Now if you run my_test_module.py as a Python script, both print() statements will be executed:
python my_test_module.py
>>> 'This is going to be printed out, no matter what'
>>> 'This is going to be printed out, only if user invokes the module as a script'
For a more comprehensive explanation, you can read What does if __name__ == '__main__' do in Python.
When there are certain statements in our module (M.py) we want to be executed when it'll be running as main (not imported), we can place those statements (test-cases, print statements) under this if block.
As by default (when module running as main, not imported) the __name__ variable is set to "__main__", and when it'll be imported the __name__ variable will get a different value, most probably the name of the module ('M').
This is helpful in running different variants of a modules together, and separating their specific input & output statements and also if there are any test-cases.
In short, use this 'if __name__ == "main" ' block to prevent (certain) code from being run when the module is imported.
Put simply, __name__ is a variable defined for each script that defines whether the script is being run as the main module or it is being run as an imported module.
So if we have two scripts;
#script1.py
print "Script 1's name: {}".format(__name__)
and
#script2.py
import script1
print "Script 2's name: {}".format(__name__)
The output from executing script1 is
Script 1's name: __main__
And the output from executing script2 is:
Script1's name is script1
Script 2's name: __main__
As you can see, __name__ tells us which code is the 'main' module.
This is great, because you can just write code and not have to worry about structural issues like in C/C++, where, if a file does not implement a 'main' function then it cannot be compiled as an executable and if it does, it cannot then be used as a library.
Say you write a Python script that does something great and you implement a boatload of functions that are useful for other purposes. If I want to use them I can just import your script and use them without executing your program (given that your code only executes within the if __name__ == "__main__": context). Whereas in C/C++ you would have to portion out those pieces into a separate module that then includes the file. Picture the situation below;
The arrows are import links. For three modules each trying to include the previous modules code there are six files (nine, counting the implementation files) and five links. This makes it difficult to include other code into a C project unless it is compiled specifically as a library. Now picture it for Python:
You write a module, and if someone wants to use your code they just import it and the __name__ variable can help to separate the executable portion of the program from the library part.
To be short, you need to know several points:
import a action actually runs all that can be run in a.py, meaning each line in a.py
Because of point 1, you may not want everything to be run in a.py when importing it
To solve the problem in point 2, Python allows you to use a condition check
__name__ is an implicit variable in all .py modules:
when a.py is imported, the value of __name__ of a.py module is set to its file name "a"
when a.py is run directly using "python a.py", the value of __name__ is set to a string __main__
Based on the mechanism how Python sets the variable __name__ for each module, do you know how to achieve point 3? The answer is fairly easy, right? Use an if condition: if __name__ == "__main__": // do A
then python a.py will run the part // do A
and import a will skip the part // do A
You can even put if __name__ == "a" depending on your functional need, but rarely do
The important thing that Python is special at is point 4! The rest is just basic logic.
I've been reading so much throughout the answers on this page. I would say, if you know the thing, for sure you will understand those answers, otherwise, you are still confused.
Let's look at the answer in a more abstract way:
Suppose we have this code in x.py:
...
<Block A>
if __name__ == '__main__':
<Block B>
...
Blocks A and B are run when we are running x.py.
But just block A (and not B) is run when we are running another module, y.py for example, in which x.py is imported and the code is run from there (like when a function in x.py is called from y.py).
When you run Python interactively the local __name__ variable is assigned a value of __main__. Likewise, when you execute a Python module from the command line, rather than importing it into another module, its __name__ attribute is assigned a value of __main__, rather than the actual name of the module. In this way, modules can look at their own __name__ value to determine for themselves how they are being used, whether as support for another program or as the main application executed from the command line. Thus, the following idiom is quite common in Python modules:
if __name__ == '__main__':
# Do something appropriate here, like calling a
# main() function defined elsewhere in this module.
main()
else:
# Do nothing. This module has been imported by another
# module that wants to make use of the functions,
# classes and other useful bits it has defined.
Consider:
if __name__ == "__main__":
main()
It checks if the __name__ attribute of the Python script is "__main__". In other words, if the program itself is executed, the attribute will be __main__, so the program will be executed (in this case the main() function).
However, if your Python script is used by a module, any code outside of the if statement will be executed, so if __name__ == "__main__" is used just to check if the program is used as a module or not, and therefore decides whether to run the code.
Before explaining anything about if __name__ == '__main__' it is important to understand what __name__ is and what it does.
What is __name__?
__name__ is a DunderAlias - can be thought of as a global variable (accessible from modules) and works in a similar way to global.
It is a string (global as mentioned above) as indicated by type(__name__) (yielding <class 'str'>), and is an inbuilt standard for both Python 3 and Python 2 versions.
Where
It can not only be used in scripts but can also be found in both the interpreter and modules/packages.
Interpreter:
>>> print(__name__)
__main__
>>>
Script:
test_file.py:
print(__name__)
Resulting in __main__
Module or package:
somefile.py:
def somefunction():
print(__name__)
test_file.py:
import somefile
somefile.somefunction()
Resulting in somefile
Notice that when used in a package or module, __name__ takes the name of the file. The path of the actual module or package path is not given, but has its own DunderAlias __file__, that allows for this.
You should see that, where __name__, where it is the main file (or program) will always return __main__, and if it is a module/package, or anything that is running off some other Python script, will return the name of the file where it has originated from.
Practice
Being a variable means that it's value can be overwritten ("can" does not mean "should"), overwriting the value of __name__ will result in a lack of readability. So do not do it, for any reason. If you need a variable define a new variable.
It is always assumed that the value of __name__ to be __main__ or the name of the file. Once again changing this default value will cause more confusion that it will do good, causing problems further down the line.
Example:
>>> __name__ = 'Horrify' # Change default from __main__
>>> if __name__ == 'Horrify': print(__name__)
...
>>> else: print('Not Horrify')
...
Horrify
>>>
It is considered good practice in general to include the if __name__ == '__main__' in scripts.
Now to answer if __name__ == '__main__':
Now we know the behaviour of __name__ things become clearer:
An if is a flow control statement that contains the block of code will execute if the value given is true. We have seen that __name__ can take either
__main__ or the file name it has been imported from.
This means that if __name__ is equal to __main__ then the file must be the main file and must actually be running (or it is the interpreter), not a module or package imported into the script.
If indeed __name__ does take the value of __main__ then whatever is in that block of code will execute.
This tells us that if the file running is the main file (or you are running from the interpreter directly) then that condition must execute. If it is a package then it should not, and the value will not be __main__.
Modules
__name__ can also be used in modules to define the name of a module
Variants
It is also possible to do other, less common but useful things with __name__, some I will show here:
Executing only if the file is a module or package
if __name__ != '__main__':
# Do some useful things
Running one condition if the file is the main one and another if it is not
if __name__ == '__main__':
# Execute something
else:
# Do some useful things
You can also use it to provide runnable help functions/utilities on packages and modules without the elaborate use of libraries.
It also allows modules to be run from the command line as main scripts, which can be also very useful.
I think it's best to break the answer in depth and in simple words:
__name__: Every module in Python has a special attribute called __name__.
It is a built-in variable that returns the name of the module.
__main__: Like other programming languages, Python too has an execution entry point, i.e., main. '__main__' is the name of the scope in which top-level code executes. Basically you have two ways of using a Python module: Run it directly as a script, or import it. When a module is run as a script, its __name__ is set to __main__.
Thus, the value of the __name__ attribute is set to __main__ when the module is run as the main program. Otherwise the value of __name__ is set to contain the name of the module.
It is a special for when a Python file is called from the command line. This is typically used to call a "main()" function or execute other appropriate startup code, like commandline arguments handling for instance.
It could be written in several ways. Another is:
def some_function_for_instance_main():
dosomething()
__name__ == '__main__' and some_function_for_instance_main()
I am not saying you should use this in production code, but it serves to illustrate that there is nothing "magical" about if __name__ == '__main__'.
It just a convention for invoking a main function in Python files.
There are a number of variables that the system (Python interpreter) provides for source files (modules). You can get their values anytime you want, so, let us focus on the __name__ variable/attribute:
When Python loads a source code file, it executes all of the code found in it. (Note that it doesn't call all of the methods and functions defined in the file, but it does define them.)
Before the interpreter executes the source code file though, it defines a few special variables for that file; __name__ is one of those special variables that Python automatically defines for each source code file.
If Python is loading this source code file as the main program (i.e. the file you run), then it sets the special __name__ variable for this file to have a value "__main__".
If this is being imported from another module, __name__ will be set to that module's name.
So, in your example in part:
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
means that the code block:
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
will be executed only when you run the module directly; the code block will not execute if another module is calling/importing it because the value of __name__ will not equal to "main" in that particular instance.
Hope this helps out.
if __name__ == "__main__": is basically the top-level script environment, and it specifies the interpreter that ('I have the highest priority to be executed first').
'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.
if __name__ == "__main__":
# Execute only if run as a script
main()
Consider:
print __name__
The output for the above is __main__.
if __name__ == "__main__":
print "direct method"
The above statement is true and prints "direct method". Suppose if they imported this class in another class it doesn't print "direct method" because, while importing, it will set __name__ equal to "first model name".
In simple words:
The code you see under if __name__ == "__main__": will only get called upon when your Python file is executed as python example1.py
However, if you wish to import your Python file example1.py as a module to work with another Python file, say example2.py, the code under if __name__ == "__main__": will not run or take any effect.
You can make the file usable as a script as well as an importable module.
fibo.py (a module named fibo)
# Other modules can IMPORT this MODULE to use the function fib
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
Reference: https://docs.python.org/3.5/tutorial/modules.html
The reason for
if __name__ == "__main__":
main()
is primarily to avoid the import lock problems that would arise from having code directly imported. You want main() to run if your file was directly invoked (that's the __name__ == "__main__" case), but if your code was imported then the importer has to enter your code from the true main module to avoid import lock problems.
A side-effect is that you automatically sign on to a methodology that supports multiple entry points. You can run your program using main() as the entry point, but you don't have to. While setup.py expects main(), other tools use alternate entry points. For example, to run your file as a gunicorn process, you define an app() function instead of a main(). Just as with setup.py, gunicorn imports your code so you don't want it do do anything while it's being imported (because of the import lock issue).
If you are a beginner, probably the only answer you need right now is that this code is unnecessary for a simple script. It is only useful if you want to be able to import your script (or unpickle etc; see the other answers here for some other non-beginner scenarios).
In slightly different words, the if __name__ guard is a mechanism for hiding code from other code. If you don't have a specific reason to hide something, don't: If you don't need to hide some code from import, don't put it behind this guard, and if you do, hide as little as possible.
In slightly more detail, let's say you have a simple script fib.py (adapted from this answer):
# XXX FIXME: useless (see below)
if __name__ == "__main__":
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, if you simply run python fib.py it works fine. But __name__ will always be "__main__" in this scenario, so the condition is actually unnecessary. The script could be simplified to just
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, you can't import fib with the new version, but if you didn't plan to do that in the first place, this version is actually better, because it's simpler and clearer.
If you do want to be able to import fib, the first version is useless, too, because the useful code is in a section which will not run when you import this file (in which case __name__ will not be "__main__"). The proper design in that case would be to refactor the code so that the useful parts are in a function you can run when you want to after you have imported it.
def main():
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
if __name__ == "__main__":
main()
Now, if you import fib, the call to main() will not be executed; but when you run python fib.py, it will.
Actually, a better design still would be to isolate the reusable part (the actual calculation) from the user-visible input/output:
def fibn(n: int) -> int:
a, b = 0, 1
while b < n:
a, b = b, a+b
return b
def main() -> None:
n = int(input('Write a number: '))
print('Fibonacci number %i: %i' % (n, fibn(n)))
if __name__ == "__main__":
main()
Now, you can from fib import fibn and call the fibn() function from the code which performs this import.
(I called the function fibn() just to make it clearer what is what in this example. In real life, you might call it fib() and do from fib import fib.)
Similarly, you could import and call the main function if you wanted to reuse it.
Returning to the code in the question, I would similarly move the code from the if into a function as well, so that callers can invoke that function if they want to.
def main():
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
if __name__ == "__main__":
main()
This changes the scope of the lock variable; if the surrounding code needs access to it, you will need to make it global (or, perhaps, better, refactor main to return lock, and have the caller capture the value in a local variable of its own).
(Unlike in languages like C, the name main has no specific meaning to Python; but it's a common convention to use it as the name of the thing which will be run. You still have to actually explicitly call it, like main(), unlike in C.)
Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.
Small example to explain in short.
Script test.py
apple = 42
def hello_world():
print("I am inside hello_world")
if __name__ == "__main__":
print("Value of __name__ is: ", __name__)
print("Going to call hello_world")
hello_world()
We can execute this directly as
python test.py
Output
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
Now suppose we call the above script from another script:
Script external_calling.py
import test
print(test.apple)
test.hello_world()
print(test.__name__)
When you execute this,
python external_calling.py
Output
42
I am inside hello_world
test
So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.
This answer is for Java programmers learning Python.
Every Java file typically contains one public class. You can use that class in two ways:
Call the class from other files. You just have to import it in the calling program.
Run the class stand alone, for testing purposes.
For the latter case, the class should contain a public static void main() method. In Python this purpose is served by the globally defined label '__main__'.
If this .py file are imported by other .py files, the code under the if statement will not be executed.
If this .py are run by python this_py.py under shell, or double clicked in Windows. the code under the if statement will be executed.
It is usually written for testing.
We see if __name__ == '__main__': quite often.
It checks if a module is being imported or not.
In other words, the code within the if block will be executed only when the code runs directly. Here directly means not imported.
Let's see what it does using a simple code that prints the name of the module:
# test.py
def test():
print('test module name=%s' %(__name__))
if __name__ == '__main__':
print('call test()')
test()
If we run the code directly via python test.py, the module name is __main__:
call test()
test module name=__main__
If the Python interpreter is running a particular module then the __name__ global variable will have the value "__main__":
def a():
print("a")
def b():
print("b")
if __name__ == "__main__":
print ("you can see me")
a()
else:
print ("You can't see me")
b()
When you run this script, it prints:
you can see me
a
If you import this file, say A to file B, and execute the file B then if __name__ == "__main__" in file A becomes False, so it prints:
You can't see me
b
All the answers have pretty much explained the functionality. But I will provide one example of its usage which might help clearing out the concept further.
Assume that you have two Python files, a.py and b.py. Now, a.py imports b.py. We run the a.py file, where the import b.py code is executed first. Before the rest of the a.py code runs, the code in the file b.py must run completely.
In the b.py code, there is some code that is exclusive to that file b.py and we don't want any other file (other than the b.py file), that has imported the b.py file, to run it.
So that is what this line of code checks. If it is the main file (i.e., b.py) running the code, which in this case it is not (a.py is the main file running), then only the code gets executed.

Python Falsk __name__ [duplicate]

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?.
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 then
it 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.
# Suppose this is foo.py.
print("before import")
import math
print("before function_a")
def function_a():
print("Function A")
print("before function_b")
def function_b():
print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
print("after __name__ guard")
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.
python foo.py
the interpreter will assign the hard-coded string "__main__" to the __name__ variable, i.e.
# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__"
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:
# Suppose this is in some other main program.
import foo
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.
# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"
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 called math. This is equivalent to replacing import math with the following (note that __import__ is a low-level function in Python that takes a string and triggers the actual import):
# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
It prints the string "before function_a".
It executes the def block, creating a function object, then assigning that function object to a variable called function_a.
It prints the string "before function_b".
It executes the second def block, creating another function object, then assigning it to a variable called function_b.
It prints the string "before __name__ guard".
Only When Your Module Is the Main Program
If your module is the main program, then it will see that __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
(instead) If your module is not the main program but was imported by another one, then __name__ will be "foo", not "__main__", and it'll skip the body of the if statement.
Always
It will print the string "after __name__ guard" in both situations.
Summary
In summary, here's what'd be printed in the two cases:
# What gets printed if foo is the main program
before import
before function_a
before function_b
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before function_a
before function_b
before __name__ guard
after __name__ guard
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 say python foo2.py on the command-line? Why?
# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo2 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
if __name__ == "__main__":
print("m1")
function_a()
print("m2")
print("t2")
Now, figure out what will happen if you remove the __name__ check in foo3.py:
# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo3 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
print("m1")
function_a()
print("m2")
print("t2")
What will this do when used as a script? When imported as a module?
# Suppose this is in foo4.py
__name__ = "__main__"
def bar():
print("bar")
print("before __name__ guard")
if __name__ == "__main__":
bar()
print("after __name__ guard")
When your script is run by passing it as a command to the Python interpreter,
python myscript.py
all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets run. Unlike other languages, there's no main() function that gets run automatically - the main() function is implicitly all the code at the top level.
In this case, the top-level code is an if block. __name__ is a built-in variable which evaluates to the name of the current module. However, if a module is being run directly (as in myscript.py above), then __name__ instead is set to the string "__main__". Thus, you can test whether your script is being run directly or being imported by something else by testing
if __name__ == "__main__":
...
If your script is being imported into another module, its various function and class definitions will be imported and its top-level code will be executed, but the code in the then-body of the if clause above won't get run as the condition is not met. As a basic example, consider the following two scripts:
# file one.py
def func():
print("func() in one.py")
print("top-level in one.py")
if __name__ == "__main__":
print("one.py is being run directly")
else:
print("one.py is being imported into another module")
# file two.py
import one
print("top-level in two.py")
one.func()
if __name__ == "__main__":
print("two.py is being run directly")
else:
print("two.py is being imported into another module")
Now, if you invoke the interpreter as
python one.py
The output will be
top-level in one.py
one.py is being run directly
If you run two.py instead:
python two.py
You get
top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly
Thus, when module one gets loaded, its __name__ equals "one" instead of "__main__".
Create the following two files:
# a.py
import b
# b.py
print("__name__ equals " + __name__)
if __name__ == '__main__':
print("if-statement was executed")
Now run each file individually.
Running python a.py:
$ python a.py
__name__ equals b
When a.py is executed, it imports the module b. This causes all the code inside b to run. Python sets globals()['__name__'] in the b module to the module's name, b.
Running python b.py:
$ python b.py
__name__ equals __main__
if-statement was executed
When only the file b.py is executed, Python sets globals()['__name__'] in this file to "__main__". Therefore, the if statement evaluates to True this time.
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?
Developing and Testing Your Code
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
The Problem
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.
A Better Way
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).
An Even Better Way
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()
if __name__ == "__main__" is the part that runs when the script is run from (say) the command line using a command like python myscript.py.
What does if __name__ == "__main__": do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
at the end of your mycode.py script,
when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
There are lots of different takes here on the mechanics of the code in question, the "How", but for me none of it made sense until I understood the "Why". This should be especially helpful for new programmers.
Take file "ab.py":
def a():
print('A function in ab file');
a()
And a second file "xy.py":
import ab
def main():
print('main function: this is where the action is')
def x():
print ('peripheral task: might be useful in other projects')
x()
if __name__ == "__main__":
main()
What is this code actually doing?
When you execute xy.py, you import ab. The import statement runs the module immediately on import, so ab's operations get executed before the remainder of xy's. Once finished with ab, it continues with xy.
The interpreter keeps track of which scripts are running with __name__. When you run a script - no matter what you've named it - the interpreter calls it "__main__", making it the master or 'home' script that gets returned to after running an external script.
Any other script that's called from this "__main__" script is assigned its filename as its __name__ (e.g., __name__ == "ab.py"). Hence, the line if __name__ == "__main__": is the interpreter's test to determine if it's interpreting/parsing the 'home' script that was initially executed, or if it's temporarily peeking into another (external) script. This gives the programmer flexibility to have the script behave differently if it's executed directly vs. called externally.
Let's step through the above code to understand what's happening, focusing first on the unindented lines and the order they appear in the scripts. Remember that function - or def - blocks don't do anything by themselves until they're called. What the interpreter might say if mumbled to itself:
Open xy.py as the 'home' file; call it "__main__" in the __name__ variable.
Import and open file with the __name__ == "ab.py".
Oh, a function. I'll remember that.
Ok, function a(); I just learned that. Printing 'A function in ab file'.
End of file; back to "__main__"!
Oh, a function. I'll remember that.
Another one.
Function x(); ok, printing 'peripheral task: might be useful in other projects'.
What's this? An if statement. Well, the condition has been met (the variable __name__ has been set to "__main__"), so I'll enter the main() function and print 'main function: this is where the action is'.
The bottom two lines mean: "If this is the "__main__" or 'home' script, execute the function called main()". That's why you'll see a def main(): block up top, which contains the main flow of the script's functionality.
Why implement this?
Remember what I said earlier about import statements? When you import a module it doesn't just 'recognize' it and wait for further instructions - it actually runs all the executable operations contained within the script. So, putting the meat of your script into the main() function effectively quarantines it, putting it in isolation so that it won't immediately run when imported by another script.
Again, there will be exceptions, but common practice is that main() doesn't usually get called externally. So you may be wondering one more thing: if we're not calling main(), why are we calling the script at all? It's because many people structure their scripts with standalone functions that are built to be run independent of the rest of the code in the file. They're then later called somewhere else in the body of the script. Which brings me to this:
But the code works without it
Yes, that's right. These separate functions can be called from an in-line script that's not contained inside a main() function. If you're accustomed (as I am, in my early learning stages of programming) to building in-line scripts that do exactly what you need, and you'll try to figure it out again if you ever need that operation again ... well, you're not used to this kind of internal structure to your code, because it's more complicated to build and it's not as intuitive to read.
But that's a script that probably can't have its functions called externally, because if it did it would immediately start calculating and assigning variables. And chances are if you're trying to re-use a function, your new script is related closely enough to the old one that there will be conflicting variables.
In splitting out independent functions, you gain the ability to re-use your previous work by calling them into another script. For example, "example.py" might import "xy.py" and call x(), making use of the 'x' function from "xy.py". (Maybe it's capitalizing the third word of a given text string; creating a NumPy array from a list of numbers and squaring them; or detrending a 3D surface. The possibilities are limitless.)
(As an aside, this question contains an answer by #kindall that finally helped me to understand - the why, not the how. Unfortunately it's been marked as a duplicate of this one, which I think is a mistake.)
The code under if __name__ == '__main__': will only be executed if the module is invoked as a script.
As an example, consider the following module my_test_module.py:
# my_test_module.py
print('This is going to be printed out, no matter what')
if __name__ == '__main__':
print('This is going to be printed out, only if user invokes the module as a script')
First possibility: Import my_test_module.py in another module
# main.py
import my_test_module
if __name__ == '__main__':
print('Hello from main.py')
Now if you invoke main.py:
python main.py
>> 'This is going to be printed out, no matter what'
>> 'Hello from main.py'
Note that only the top-level print() statement in my_test_module is executed.
Second possibility: Invoke my_test_module.py as a script
Now if you run my_test_module.py as a Python script, both print() statements will be executed:
python my_test_module.py
>>> 'This is going to be printed out, no matter what'
>>> 'This is going to be printed out, only if user invokes the module as a script'
For a more comprehensive explanation, you can read What does if __name__ == '__main__' do in Python.
When there are certain statements in our module (M.py) we want to be executed when it'll be running as main (not imported), we can place those statements (test-cases, print statements) under this if block.
As by default (when module running as main, not imported) the __name__ variable is set to "__main__", and when it'll be imported the __name__ variable will get a different value, most probably the name of the module ('M').
This is helpful in running different variants of a modules together, and separating their specific input & output statements and also if there are any test-cases.
In short, use this 'if __name__ == "main" ' block to prevent (certain) code from being run when the module is imported.
Put simply, __name__ is a variable defined for each script that defines whether the script is being run as the main module or it is being run as an imported module.
So if we have two scripts;
#script1.py
print "Script 1's name: {}".format(__name__)
and
#script2.py
import script1
print "Script 2's name: {}".format(__name__)
The output from executing script1 is
Script 1's name: __main__
And the output from executing script2 is:
Script1's name is script1
Script 2's name: __main__
As you can see, __name__ tells us which code is the 'main' module.
This is great, because you can just write code and not have to worry about structural issues like in C/C++, where, if a file does not implement a 'main' function then it cannot be compiled as an executable and if it does, it cannot then be used as a library.
Say you write a Python script that does something great and you implement a boatload of functions that are useful for other purposes. If I want to use them I can just import your script and use them without executing your program (given that your code only executes within the if __name__ == "__main__": context). Whereas in C/C++ you would have to portion out those pieces into a separate module that then includes the file. Picture the situation below;
The arrows are import links. For three modules each trying to include the previous modules code there are six files (nine, counting the implementation files) and five links. This makes it difficult to include other code into a C project unless it is compiled specifically as a library. Now picture it for Python:
You write a module, and if someone wants to use your code they just import it and the __name__ variable can help to separate the executable portion of the program from the library part.
To be short, you need to know several points:
import a action actually runs all that can be run in a.py, meaning each line in a.py
Because of point 1, you may not want everything to be run in a.py when importing it
To solve the problem in point 2, Python allows you to use a condition check
__name__ is an implicit variable in all .py modules:
when a.py is imported, the value of __name__ of a.py module is set to its file name "a"
when a.py is run directly using "python a.py", the value of __name__ is set to a string __main__
Based on the mechanism how Python sets the variable __name__ for each module, do you know how to achieve point 3? The answer is fairly easy, right? Use an if condition: if __name__ == "__main__": // do A
then python a.py will run the part // do A
and import a will skip the part // do A
You can even put if __name__ == "a" depending on your functional need, but rarely do
The important thing that Python is special at is point 4! The rest is just basic logic.
I've been reading so much throughout the answers on this page. I would say, if you know the thing, for sure you will understand those answers, otherwise, you are still confused.
Let's look at the answer in a more abstract way:
Suppose we have this code in x.py:
...
<Block A>
if __name__ == '__main__':
<Block B>
...
Blocks A and B are run when we are running x.py.
But just block A (and not B) is run when we are running another module, y.py for example, in which x.py is imported and the code is run from there (like when a function in x.py is called from y.py).
When you run Python interactively the local __name__ variable is assigned a value of __main__. Likewise, when you execute a Python module from the command line, rather than importing it into another module, its __name__ attribute is assigned a value of __main__, rather than the actual name of the module. In this way, modules can look at their own __name__ value to determine for themselves how they are being used, whether as support for another program or as the main application executed from the command line. Thus, the following idiom is quite common in Python modules:
if __name__ == '__main__':
# Do something appropriate here, like calling a
# main() function defined elsewhere in this module.
main()
else:
# Do nothing. This module has been imported by another
# module that wants to make use of the functions,
# classes and other useful bits it has defined.
Consider:
if __name__ == "__main__":
main()
It checks if the __name__ attribute of the Python script is "__main__". In other words, if the program itself is executed, the attribute will be __main__, so the program will be executed (in this case the main() function).
However, if your Python script is used by a module, any code outside of the if statement will be executed, so if __name__ == "__main__" is used just to check if the program is used as a module or not, and therefore decides whether to run the code.
Before explaining anything about if __name__ == '__main__' it is important to understand what __name__ is and what it does.
What is __name__?
__name__ is a DunderAlias - can be thought of as a global variable (accessible from modules) and works in a similar way to global.
It is a string (global as mentioned above) as indicated by type(__name__) (yielding <class 'str'>), and is an inbuilt standard for both Python 3 and Python 2 versions.
Where
It can not only be used in scripts but can also be found in both the interpreter and modules/packages.
Interpreter:
>>> print(__name__)
__main__
>>>
Script:
test_file.py:
print(__name__)
Resulting in __main__
Module or package:
somefile.py:
def somefunction():
print(__name__)
test_file.py:
import somefile
somefile.somefunction()
Resulting in somefile
Notice that when used in a package or module, __name__ takes the name of the file. The path of the actual module or package path is not given, but has its own DunderAlias __file__, that allows for this.
You should see that, where __name__, where it is the main file (or program) will always return __main__, and if it is a module/package, or anything that is running off some other Python script, will return the name of the file where it has originated from.
Practice
Being a variable means that it's value can be overwritten ("can" does not mean "should"), overwriting the value of __name__ will result in a lack of readability. So do not do it, for any reason. If you need a variable define a new variable.
It is always assumed that the value of __name__ to be __main__ or the name of the file. Once again changing this default value will cause more confusion that it will do good, causing problems further down the line.
Example:
>>> __name__ = 'Horrify' # Change default from __main__
>>> if __name__ == 'Horrify': print(__name__)
...
>>> else: print('Not Horrify')
...
Horrify
>>>
It is considered good practice in general to include the if __name__ == '__main__' in scripts.
Now to answer if __name__ == '__main__':
Now we know the behaviour of __name__ things become clearer:
An if is a flow control statement that contains the block of code will execute if the value given is true. We have seen that __name__ can take either
__main__ or the file name it has been imported from.
This means that if __name__ is equal to __main__ then the file must be the main file and must actually be running (or it is the interpreter), not a module or package imported into the script.
If indeed __name__ does take the value of __main__ then whatever is in that block of code will execute.
This tells us that if the file running is the main file (or you are running from the interpreter directly) then that condition must execute. If it is a package then it should not, and the value will not be __main__.
Modules
__name__ can also be used in modules to define the name of a module
Variants
It is also possible to do other, less common but useful things with __name__, some I will show here:
Executing only if the file is a module or package
if __name__ != '__main__':
# Do some useful things
Running one condition if the file is the main one and another if it is not
if __name__ == '__main__':
# Execute something
else:
# Do some useful things
You can also use it to provide runnable help functions/utilities on packages and modules without the elaborate use of libraries.
It also allows modules to be run from the command line as main scripts, which can be also very useful.
I think it's best to break the answer in depth and in simple words:
__name__: Every module in Python has a special attribute called __name__.
It is a built-in variable that returns the name of the module.
__main__: Like other programming languages, Python too has an execution entry point, i.e., main. '__main__' is the name of the scope in which top-level code executes. Basically you have two ways of using a Python module: Run it directly as a script, or import it. When a module is run as a script, its __name__ is set to __main__.
Thus, the value of the __name__ attribute is set to __main__ when the module is run as the main program. Otherwise the value of __name__ is set to contain the name of the module.
It is a special for when a Python file is called from the command line. This is typically used to call a "main()" function or execute other appropriate startup code, like commandline arguments handling for instance.
It could be written in several ways. Another is:
def some_function_for_instance_main():
dosomething()
__name__ == '__main__' and some_function_for_instance_main()
I am not saying you should use this in production code, but it serves to illustrate that there is nothing "magical" about if __name__ == '__main__'.
It just a convention for invoking a main function in Python files.
There are a number of variables that the system (Python interpreter) provides for source files (modules). You can get their values anytime you want, so, let us focus on the __name__ variable/attribute:
When Python loads a source code file, it executes all of the code found in it. (Note that it doesn't call all of the methods and functions defined in the file, but it does define them.)
Before the interpreter executes the source code file though, it defines a few special variables for that file; __name__ is one of those special variables that Python automatically defines for each source code file.
If Python is loading this source code file as the main program (i.e. the file you run), then it sets the special __name__ variable for this file to have a value "__main__".
If this is being imported from another module, __name__ will be set to that module's name.
So, in your example in part:
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
means that the code block:
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
will be executed only when you run the module directly; the code block will not execute if another module is calling/importing it because the value of __name__ will not equal to "main" in that particular instance.
Hope this helps out.
if __name__ == "__main__": is basically the top-level script environment, and it specifies the interpreter that ('I have the highest priority to be executed first').
'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.
if __name__ == "__main__":
# Execute only if run as a script
main()
Consider:
print __name__
The output for the above is __main__.
if __name__ == "__main__":
print "direct method"
The above statement is true and prints "direct method". Suppose if they imported this class in another class it doesn't print "direct method" because, while importing, it will set __name__ equal to "first model name".
In simple words:
The code you see under if __name__ == "__main__": will only get called upon when your Python file is executed as python example1.py
However, if you wish to import your Python file example1.py as a module to work with another Python file, say example2.py, the code under if __name__ == "__main__": will not run or take any effect.
You can make the file usable as a script as well as an importable module.
fibo.py (a module named fibo)
# Other modules can IMPORT this MODULE to use the function fib
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
Reference: https://docs.python.org/3.5/tutorial/modules.html
The reason for
if __name__ == "__main__":
main()
is primarily to avoid the import lock problems that would arise from having code directly imported. You want main() to run if your file was directly invoked (that's the __name__ == "__main__" case), but if your code was imported then the importer has to enter your code from the true main module to avoid import lock problems.
A side-effect is that you automatically sign on to a methodology that supports multiple entry points. You can run your program using main() as the entry point, but you don't have to. While setup.py expects main(), other tools use alternate entry points. For example, to run your file as a gunicorn process, you define an app() function instead of a main(). Just as with setup.py, gunicorn imports your code so you don't want it do do anything while it's being imported (because of the import lock issue).
If you are a beginner, probably the only answer you need right now is that this code is unnecessary for a simple script. It is only useful if you want to be able to import your script (or unpickle etc; see the other answers here for some other non-beginner scenarios).
In slightly different words, the if __name__ guard is a mechanism for hiding code from other code. If you don't have a specific reason to hide something, don't: If you don't need to hide some code from import, don't put it behind this guard, and if you do, hide as little as possible.
In slightly more detail, let's say you have a simple script fib.py (adapted from this answer):
# XXX FIXME: useless (see below)
if __name__ == "__main__":
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, if you simply run python fib.py it works fine. But __name__ will always be "__main__" in this scenario, so the condition is actually unnecessary. The script could be simplified to just
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, you can't import fib with the new version, but if you didn't plan to do that in the first place, this version is actually better, because it's simpler and clearer.
If you do want to be able to import fib, the first version is useless, too, because the useful code is in a section which will not run when you import this file (in which case __name__ will not be "__main__"). The proper design in that case would be to refactor the code so that the useful parts are in a function you can run when you want to after you have imported it.
def main():
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
if __name__ == "__main__":
main()
Now, if you import fib, the call to main() will not be executed; but when you run python fib.py, it will.
Actually, a better design still would be to isolate the reusable part (the actual calculation) from the user-visible input/output:
def fibn(n: int) -> int:
a, b = 0, 1
while b < n:
a, b = b, a+b
return b
def main() -> None:
n = int(input('Write a number: '))
print('Fibonacci number %i: %i' % (n, fibn(n)))
if __name__ == "__main__":
main()
Now, you can from fib import fibn and call the fibn() function from the code which performs this import.
(I called the function fibn() just to make it clearer what is what in this example. In real life, you might call it fib() and do from fib import fib.)
Similarly, you could import and call the main function if you wanted to reuse it.
Returning to the code in the question, I would similarly move the code from the if into a function as well, so that callers can invoke that function if they want to.
def main():
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
if __name__ == "__main__":
main()
This changes the scope of the lock variable; if the surrounding code needs access to it, you will need to make it global (or, perhaps, better, refactor main to return lock, and have the caller capture the value in a local variable of its own).
(Unlike in languages like C, the name main has no specific meaning to Python; but it's a common convention to use it as the name of the thing which will be run. You still have to actually explicitly call it, like main(), unlike in C.)
Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.
Small example to explain in short.
Script test.py
apple = 42
def hello_world():
print("I am inside hello_world")
if __name__ == "__main__":
print("Value of __name__ is: ", __name__)
print("Going to call hello_world")
hello_world()
We can execute this directly as
python test.py
Output
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
Now suppose we call the above script from another script:
Script external_calling.py
import test
print(test.apple)
test.hello_world()
print(test.__name__)
When you execute this,
python external_calling.py
Output
42
I am inside hello_world
test
So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.
This answer is for Java programmers learning Python.
Every Java file typically contains one public class. You can use that class in two ways:
Call the class from other files. You just have to import it in the calling program.
Run the class stand alone, for testing purposes.
For the latter case, the class should contain a public static void main() method. In Python this purpose is served by the globally defined label '__main__'.
If this .py file are imported by other .py files, the code under the if statement will not be executed.
If this .py are run by python this_py.py under shell, or double clicked in Windows. the code under the if statement will be executed.
It is usually written for testing.
We see if __name__ == '__main__': quite often.
It checks if a module is being imported or not.
In other words, the code within the if block will be executed only when the code runs directly. Here directly means not imported.
Let's see what it does using a simple code that prints the name of the module:
# test.py
def test():
print('test module name=%s' %(__name__))
if __name__ == '__main__':
print('call test()')
test()
If we run the code directly via python test.py, the module name is __main__:
call test()
test module name=__main__
If the Python interpreter is running a particular module then the __name__ global variable will have the value "__main__":
def a():
print("a")
def b():
print("b")
if __name__ == "__main__":
print ("you can see me")
a()
else:
print ("You can't see me")
b()
When you run this script, it prints:
you can see me
a
If you import this file, say A to file B, and execute the file B then if __name__ == "__main__" in file A becomes False, so it prints:
You can't see me
b
All the answers have pretty much explained the functionality. But I will provide one example of its usage which might help clearing out the concept further.
Assume that you have two Python files, a.py and b.py. Now, a.py imports b.py. We run the a.py file, where the import b.py code is executed first. Before the rest of the a.py code runs, the code in the file b.py must run completely.
In the b.py code, there is some code that is exclusive to that file b.py and we don't want any other file (other than the b.py file), that has imported the b.py file, to run it.
So that is what this line of code checks. If it is the main file (i.e., b.py) running the code, which in this case it is not (a.py is the main file running), then only the code gets executed.

pytest pep8 compliance check gets stuck [duplicate]

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?.
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 then
it 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.
# Suppose this is foo.py.
print("before import")
import math
print("before function_a")
def function_a():
print("Function A")
print("before function_b")
def function_b():
print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
print("after __name__ guard")
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.
python foo.py
the interpreter will assign the hard-coded string "__main__" to the __name__ variable, i.e.
# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__"
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:
# Suppose this is in some other main program.
import foo
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.
# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"
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 called math. This is equivalent to replacing import math with the following (note that __import__ is a low-level function in Python that takes a string and triggers the actual import):
# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
It prints the string "before function_a".
It executes the def block, creating a function object, then assigning that function object to a variable called function_a.
It prints the string "before function_b".
It executes the second def block, creating another function object, then assigning it to a variable called function_b.
It prints the string "before __name__ guard".
Only When Your Module Is the Main Program
If your module is the main program, then it will see that __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
(instead) If your module is not the main program but was imported by another one, then __name__ will be "foo", not "__main__", and it'll skip the body of the if statement.
Always
It will print the string "after __name__ guard" in both situations.
Summary
In summary, here's what'd be printed in the two cases:
# What gets printed if foo is the main program
before import
before function_a
before function_b
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before function_a
before function_b
before __name__ guard
after __name__ guard
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 say python foo2.py on the command-line? Why?
# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo2 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
if __name__ == "__main__":
print("m1")
function_a()
print("m2")
print("t2")
Now, figure out what will happen if you remove the __name__ check in foo3.py:
# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo3 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
print("m1")
function_a()
print("m2")
print("t2")
What will this do when used as a script? When imported as a module?
# Suppose this is in foo4.py
__name__ = "__main__"
def bar():
print("bar")
print("before __name__ guard")
if __name__ == "__main__":
bar()
print("after __name__ guard")
When your script is run by passing it as a command to the Python interpreter,
python myscript.py
all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets run. Unlike other languages, there's no main() function that gets run automatically - the main() function is implicitly all the code at the top level.
In this case, the top-level code is an if block. __name__ is a built-in variable which evaluates to the name of the current module. However, if a module is being run directly (as in myscript.py above), then __name__ instead is set to the string "__main__". Thus, you can test whether your script is being run directly or being imported by something else by testing
if __name__ == "__main__":
...
If your script is being imported into another module, its various function and class definitions will be imported and its top-level code will be executed, but the code in the then-body of the if clause above won't get run as the condition is not met. As a basic example, consider the following two scripts:
# file one.py
def func():
print("func() in one.py")
print("top-level in one.py")
if __name__ == "__main__":
print("one.py is being run directly")
else:
print("one.py is being imported into another module")
# file two.py
import one
print("top-level in two.py")
one.func()
if __name__ == "__main__":
print("two.py is being run directly")
else:
print("two.py is being imported into another module")
Now, if you invoke the interpreter as
python one.py
The output will be
top-level in one.py
one.py is being run directly
If you run two.py instead:
python two.py
You get
top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly
Thus, when module one gets loaded, its __name__ equals "one" instead of "__main__".
Create the following two files:
# a.py
import b
# b.py
print("__name__ equals " + __name__)
if __name__ == '__main__':
print("if-statement was executed")
Now run each file individually.
Running python a.py:
$ python a.py
__name__ equals b
When a.py is executed, it imports the module b. This causes all the code inside b to run. Python sets globals()['__name__'] in the b module to the module's name, b.
Running python b.py:
$ python b.py
__name__ equals __main__
if-statement was executed
When only the file b.py is executed, Python sets globals()['__name__'] in this file to "__main__". Therefore, the if statement evaluates to True this time.
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?
Developing and Testing Your Code
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
The Problem
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.
A Better Way
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).
An Even Better Way
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()
if __name__ == "__main__" is the part that runs when the script is run from (say) the command line using a command like python myscript.py.
What does if __name__ == "__main__": do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
at the end of your mycode.py script,
when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
There are lots of different takes here on the mechanics of the code in question, the "How", but for me none of it made sense until I understood the "Why". This should be especially helpful for new programmers.
Take file "ab.py":
def a():
print('A function in ab file');
a()
And a second file "xy.py":
import ab
def main():
print('main function: this is where the action is')
def x():
print ('peripheral task: might be useful in other projects')
x()
if __name__ == "__main__":
main()
What is this code actually doing?
When you execute xy.py, you import ab. The import statement runs the module immediately on import, so ab's operations get executed before the remainder of xy's. Once finished with ab, it continues with xy.
The interpreter keeps track of which scripts are running with __name__. When you run a script - no matter what you've named it - the interpreter calls it "__main__", making it the master or 'home' script that gets returned to after running an external script.
Any other script that's called from this "__main__" script is assigned its filename as its __name__ (e.g., __name__ == "ab.py"). Hence, the line if __name__ == "__main__": is the interpreter's test to determine if it's interpreting/parsing the 'home' script that was initially executed, or if it's temporarily peeking into another (external) script. This gives the programmer flexibility to have the script behave differently if it's executed directly vs. called externally.
Let's step through the above code to understand what's happening, focusing first on the unindented lines and the order they appear in the scripts. Remember that function - or def - blocks don't do anything by themselves until they're called. What the interpreter might say if mumbled to itself:
Open xy.py as the 'home' file; call it "__main__" in the __name__ variable.
Import and open file with the __name__ == "ab.py".
Oh, a function. I'll remember that.
Ok, function a(); I just learned that. Printing 'A function in ab file'.
End of file; back to "__main__"!
Oh, a function. I'll remember that.
Another one.
Function x(); ok, printing 'peripheral task: might be useful in other projects'.
What's this? An if statement. Well, the condition has been met (the variable __name__ has been set to "__main__"), so I'll enter the main() function and print 'main function: this is where the action is'.
The bottom two lines mean: "If this is the "__main__" or 'home' script, execute the function called main()". That's why you'll see a def main(): block up top, which contains the main flow of the script's functionality.
Why implement this?
Remember what I said earlier about import statements? When you import a module it doesn't just 'recognize' it and wait for further instructions - it actually runs all the executable operations contained within the script. So, putting the meat of your script into the main() function effectively quarantines it, putting it in isolation so that it won't immediately run when imported by another script.
Again, there will be exceptions, but common practice is that main() doesn't usually get called externally. So you may be wondering one more thing: if we're not calling main(), why are we calling the script at all? It's because many people structure their scripts with standalone functions that are built to be run independent of the rest of the code in the file. They're then later called somewhere else in the body of the script. Which brings me to this:
But the code works without it
Yes, that's right. These separate functions can be called from an in-line script that's not contained inside a main() function. If you're accustomed (as I am, in my early learning stages of programming) to building in-line scripts that do exactly what you need, and you'll try to figure it out again if you ever need that operation again ... well, you're not used to this kind of internal structure to your code, because it's more complicated to build and it's not as intuitive to read.
But that's a script that probably can't have its functions called externally, because if it did it would immediately start calculating and assigning variables. And chances are if you're trying to re-use a function, your new script is related closely enough to the old one that there will be conflicting variables.
In splitting out independent functions, you gain the ability to re-use your previous work by calling them into another script. For example, "example.py" might import "xy.py" and call x(), making use of the 'x' function from "xy.py". (Maybe it's capitalizing the third word of a given text string; creating a NumPy array from a list of numbers and squaring them; or detrending a 3D surface. The possibilities are limitless.)
(As an aside, this question contains an answer by #kindall that finally helped me to understand - the why, not the how. Unfortunately it's been marked as a duplicate of this one, which I think is a mistake.)
The code under if __name__ == '__main__': will only be executed if the module is invoked as a script.
As an example, consider the following module my_test_module.py:
# my_test_module.py
print('This is going to be printed out, no matter what')
if __name__ == '__main__':
print('This is going to be printed out, only if user invokes the module as a script')
First possibility: Import my_test_module.py in another module
# main.py
import my_test_module
if __name__ == '__main__':
print('Hello from main.py')
Now if you invoke main.py:
python main.py
>> 'This is going to be printed out, no matter what'
>> 'Hello from main.py'
Note that only the top-level print() statement in my_test_module is executed.
Second possibility: Invoke my_test_module.py as a script
Now if you run my_test_module.py as a Python script, both print() statements will be executed:
python my_test_module.py
>>> 'This is going to be printed out, no matter what'
>>> 'This is going to be printed out, only if user invokes the module as a script'
For a more comprehensive explanation, you can read What does if __name__ == '__main__' do in Python.
When there are certain statements in our module (M.py) we want to be executed when it'll be running as main (not imported), we can place those statements (test-cases, print statements) under this if block.
As by default (when module running as main, not imported) the __name__ variable is set to "__main__", and when it'll be imported the __name__ variable will get a different value, most probably the name of the module ('M').
This is helpful in running different variants of a modules together, and separating their specific input & output statements and also if there are any test-cases.
In short, use this 'if __name__ == "main" ' block to prevent (certain) code from being run when the module is imported.
Put simply, __name__ is a variable defined for each script that defines whether the script is being run as the main module or it is being run as an imported module.
So if we have two scripts;
#script1.py
print "Script 1's name: {}".format(__name__)
and
#script2.py
import script1
print "Script 2's name: {}".format(__name__)
The output from executing script1 is
Script 1's name: __main__
And the output from executing script2 is:
Script1's name is script1
Script 2's name: __main__
As you can see, __name__ tells us which code is the 'main' module.
This is great, because you can just write code and not have to worry about structural issues like in C/C++, where, if a file does not implement a 'main' function then it cannot be compiled as an executable and if it does, it cannot then be used as a library.
Say you write a Python script that does something great and you implement a boatload of functions that are useful for other purposes. If I want to use them I can just import your script and use them without executing your program (given that your code only executes within the if __name__ == "__main__": context). Whereas in C/C++ you would have to portion out those pieces into a separate module that then includes the file. Picture the situation below;
The arrows are import links. For three modules each trying to include the previous modules code there are six files (nine, counting the implementation files) and five links. This makes it difficult to include other code into a C project unless it is compiled specifically as a library. Now picture it for Python:
You write a module, and if someone wants to use your code they just import it and the __name__ variable can help to separate the executable portion of the program from the library part.
To be short, you need to know several points:
import a action actually runs all that can be run in a.py, meaning each line in a.py
Because of point 1, you may not want everything to be run in a.py when importing it
To solve the problem in point 2, Python allows you to use a condition check
__name__ is an implicit variable in all .py modules:
when a.py is imported, the value of __name__ of a.py module is set to its file name "a"
when a.py is run directly using "python a.py", the value of __name__ is set to a string __main__
Based on the mechanism how Python sets the variable __name__ for each module, do you know how to achieve point 3? The answer is fairly easy, right? Use an if condition: if __name__ == "__main__": // do A
then python a.py will run the part // do A
and import a will skip the part // do A
You can even put if __name__ == "a" depending on your functional need, but rarely do
The important thing that Python is special at is point 4! The rest is just basic logic.
I've been reading so much throughout the answers on this page. I would say, if you know the thing, for sure you will understand those answers, otherwise, you are still confused.
Let's look at the answer in a more abstract way:
Suppose we have this code in x.py:
...
<Block A>
if __name__ == '__main__':
<Block B>
...
Blocks A and B are run when we are running x.py.
But just block A (and not B) is run when we are running another module, y.py for example, in which x.py is imported and the code is run from there (like when a function in x.py is called from y.py).
When you run Python interactively the local __name__ variable is assigned a value of __main__. Likewise, when you execute a Python module from the command line, rather than importing it into another module, its __name__ attribute is assigned a value of __main__, rather than the actual name of the module. In this way, modules can look at their own __name__ value to determine for themselves how they are being used, whether as support for another program or as the main application executed from the command line. Thus, the following idiom is quite common in Python modules:
if __name__ == '__main__':
# Do something appropriate here, like calling a
# main() function defined elsewhere in this module.
main()
else:
# Do nothing. This module has been imported by another
# module that wants to make use of the functions,
# classes and other useful bits it has defined.
Consider:
if __name__ == "__main__":
main()
It checks if the __name__ attribute of the Python script is "__main__". In other words, if the program itself is executed, the attribute will be __main__, so the program will be executed (in this case the main() function).
However, if your Python script is used by a module, any code outside of the if statement will be executed, so if __name__ == "__main__" is used just to check if the program is used as a module or not, and therefore decides whether to run the code.
Before explaining anything about if __name__ == '__main__' it is important to understand what __name__ is and what it does.
What is __name__?
__name__ is a DunderAlias - can be thought of as a global variable (accessible from modules) and works in a similar way to global.
It is a string (global as mentioned above) as indicated by type(__name__) (yielding <class 'str'>), and is an inbuilt standard for both Python 3 and Python 2 versions.
Where
It can not only be used in scripts but can also be found in both the interpreter and modules/packages.
Interpreter:
>>> print(__name__)
__main__
>>>
Script:
test_file.py:
print(__name__)
Resulting in __main__
Module or package:
somefile.py:
def somefunction():
print(__name__)
test_file.py:
import somefile
somefile.somefunction()
Resulting in somefile
Notice that when used in a package or module, __name__ takes the name of the file. The path of the actual module or package path is not given, but has its own DunderAlias __file__, that allows for this.
You should see that, where __name__, where it is the main file (or program) will always return __main__, and if it is a module/package, or anything that is running off some other Python script, will return the name of the file where it has originated from.
Practice
Being a variable means that it's value can be overwritten ("can" does not mean "should"), overwriting the value of __name__ will result in a lack of readability. So do not do it, for any reason. If you need a variable define a new variable.
It is always assumed that the value of __name__ to be __main__ or the name of the file. Once again changing this default value will cause more confusion that it will do good, causing problems further down the line.
Example:
>>> __name__ = 'Horrify' # Change default from __main__
>>> if __name__ == 'Horrify': print(__name__)
...
>>> else: print('Not Horrify')
...
Horrify
>>>
It is considered good practice in general to include the if __name__ == '__main__' in scripts.
Now to answer if __name__ == '__main__':
Now we know the behaviour of __name__ things become clearer:
An if is a flow control statement that contains the block of code will execute if the value given is true. We have seen that __name__ can take either
__main__ or the file name it has been imported from.
This means that if __name__ is equal to __main__ then the file must be the main file and must actually be running (or it is the interpreter), not a module or package imported into the script.
If indeed __name__ does take the value of __main__ then whatever is in that block of code will execute.
This tells us that if the file running is the main file (or you are running from the interpreter directly) then that condition must execute. If it is a package then it should not, and the value will not be __main__.
Modules
__name__ can also be used in modules to define the name of a module
Variants
It is also possible to do other, less common but useful things with __name__, some I will show here:
Executing only if the file is a module or package
if __name__ != '__main__':
# Do some useful things
Running one condition if the file is the main one and another if it is not
if __name__ == '__main__':
# Execute something
else:
# Do some useful things
You can also use it to provide runnable help functions/utilities on packages and modules without the elaborate use of libraries.
It also allows modules to be run from the command line as main scripts, which can be also very useful.
I think it's best to break the answer in depth and in simple words:
__name__: Every module in Python has a special attribute called __name__.
It is a built-in variable that returns the name of the module.
__main__: Like other programming languages, Python too has an execution entry point, i.e., main. '__main__' is the name of the scope in which top-level code executes. Basically you have two ways of using a Python module: Run it directly as a script, or import it. When a module is run as a script, its __name__ is set to __main__.
Thus, the value of the __name__ attribute is set to __main__ when the module is run as the main program. Otherwise the value of __name__ is set to contain the name of the module.
It is a special for when a Python file is called from the command line. This is typically used to call a "main()" function or execute other appropriate startup code, like commandline arguments handling for instance.
It could be written in several ways. Another is:
def some_function_for_instance_main():
dosomething()
__name__ == '__main__' and some_function_for_instance_main()
I am not saying you should use this in production code, but it serves to illustrate that there is nothing "magical" about if __name__ == '__main__'.
It just a convention for invoking a main function in Python files.
There are a number of variables that the system (Python interpreter) provides for source files (modules). You can get their values anytime you want, so, let us focus on the __name__ variable/attribute:
When Python loads a source code file, it executes all of the code found in it. (Note that it doesn't call all of the methods and functions defined in the file, but it does define them.)
Before the interpreter executes the source code file though, it defines a few special variables for that file; __name__ is one of those special variables that Python automatically defines for each source code file.
If Python is loading this source code file as the main program (i.e. the file you run), then it sets the special __name__ variable for this file to have a value "__main__".
If this is being imported from another module, __name__ will be set to that module's name.
So, in your example in part:
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
means that the code block:
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
will be executed only when you run the module directly; the code block will not execute if another module is calling/importing it because the value of __name__ will not equal to "main" in that particular instance.
Hope this helps out.
if __name__ == "__main__": is basically the top-level script environment, and it specifies the interpreter that ('I have the highest priority to be executed first').
'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.
if __name__ == "__main__":
# Execute only if run as a script
main()
Consider:
print __name__
The output for the above is __main__.
if __name__ == "__main__":
print "direct method"
The above statement is true and prints "direct method". Suppose if they imported this class in another class it doesn't print "direct method" because, while importing, it will set __name__ equal to "first model name".
In simple words:
The code you see under if __name__ == "__main__": will only get called upon when your Python file is executed as python example1.py
However, if you wish to import your Python file example1.py as a module to work with another Python file, say example2.py, the code under if __name__ == "__main__": will not run or take any effect.
You can make the file usable as a script as well as an importable module.
fibo.py (a module named fibo)
# Other modules can IMPORT this MODULE to use the function fib
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
Reference: https://docs.python.org/3.5/tutorial/modules.html
The reason for
if __name__ == "__main__":
main()
is primarily to avoid the import lock problems that would arise from having code directly imported. You want main() to run if your file was directly invoked (that's the __name__ == "__main__" case), but if your code was imported then the importer has to enter your code from the true main module to avoid import lock problems.
A side-effect is that you automatically sign on to a methodology that supports multiple entry points. You can run your program using main() as the entry point, but you don't have to. While setup.py expects main(), other tools use alternate entry points. For example, to run your file as a gunicorn process, you define an app() function instead of a main(). Just as with setup.py, gunicorn imports your code so you don't want it do do anything while it's being imported (because of the import lock issue).
If you are a beginner, probably the only answer you need right now is that this code is unnecessary for a simple script. It is only useful if you want to be able to import your script (or unpickle etc; see the other answers here for some other non-beginner scenarios).
In slightly different words, the if __name__ guard is a mechanism for hiding code from other code. If you don't have a specific reason to hide something, don't: If you don't need to hide some code from import, don't put it behind this guard, and if you do, hide as little as possible.
In slightly more detail, let's say you have a simple script fib.py (adapted from this answer):
# XXX FIXME: useless (see below)
if __name__ == "__main__":
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, if you simply run python fib.py it works fine. But __name__ will always be "__main__" in this scenario, so the condition is actually unnecessary. The script could be simplified to just
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, you can't import fib with the new version, but if you didn't plan to do that in the first place, this version is actually better, because it's simpler and clearer.
If you do want to be able to import fib, the first version is useless, too, because the useful code is in a section which will not run when you import this file (in which case __name__ will not be "__main__"). The proper design in that case would be to refactor the code so that the useful parts are in a function you can run when you want to after you have imported it.
def main():
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
if __name__ == "__main__":
main()
Now, if you import fib, the call to main() will not be executed; but when you run python fib.py, it will.
Actually, a better design still would be to isolate the reusable part (the actual calculation) from the user-visible input/output:
def fibn(n: int) -> int:
a, b = 0, 1
while b < n:
a, b = b, a+b
return b
def main() -> None:
n = int(input('Write a number: '))
print('Fibonacci number %i: %i' % (n, fibn(n)))
if __name__ == "__main__":
main()
Now, you can from fib import fibn and call the fibn() function from the code which performs this import.
(I called the function fibn() just to make it clearer what is what in this example. In real life, you might call it fib() and do from fib import fib.)
Similarly, you could import and call the main function if you wanted to reuse it.
Returning to the code in the question, I would similarly move the code from the if into a function as well, so that callers can invoke that function if they want to.
def main():
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
if __name__ == "__main__":
main()
This changes the scope of the lock variable; if the surrounding code needs access to it, you will need to make it global (or, perhaps, better, refactor main to return lock, and have the caller capture the value in a local variable of its own).
(Unlike in languages like C, the name main has no specific meaning to Python; but it's a common convention to use it as the name of the thing which will be run. You still have to actually explicitly call it, like main(), unlike in C.)
Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.
Small example to explain in short.
Script test.py
apple = 42
def hello_world():
print("I am inside hello_world")
if __name__ == "__main__":
print("Value of __name__ is: ", __name__)
print("Going to call hello_world")
hello_world()
We can execute this directly as
python test.py
Output
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
Now suppose we call the above script from another script:
Script external_calling.py
import test
print(test.apple)
test.hello_world()
print(test.__name__)
When you execute this,
python external_calling.py
Output
42
I am inside hello_world
test
So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.
This answer is for Java programmers learning Python.
Every Java file typically contains one public class. You can use that class in two ways:
Call the class from other files. You just have to import it in the calling program.
Run the class stand alone, for testing purposes.
For the latter case, the class should contain a public static void main() method. In Python this purpose is served by the globally defined label '__main__'.
If this .py file are imported by other .py files, the code under the if statement will not be executed.
If this .py are run by python this_py.py under shell, or double clicked in Windows. the code under the if statement will be executed.
It is usually written for testing.
We see if __name__ == '__main__': quite often.
It checks if a module is being imported or not.
In other words, the code within the if block will be executed only when the code runs directly. Here directly means not imported.
Let's see what it does using a simple code that prints the name of the module:
# test.py
def test():
print('test module name=%s' %(__name__))
if __name__ == '__main__':
print('call test()')
test()
If we run the code directly via python test.py, the module name is __main__:
call test()
test module name=__main__
If the Python interpreter is running a particular module then the __name__ global variable will have the value "__main__":
def a():
print("a")
def b():
print("b")
if __name__ == "__main__":
print ("you can see me")
a()
else:
print ("You can't see me")
b()
When you run this script, it prints:
you can see me
a
If you import this file, say A to file B, and execute the file B then if __name__ == "__main__" in file A becomes False, so it prints:
You can't see me
b
All the answers have pretty much explained the functionality. But I will provide one example of its usage which might help clearing out the concept further.
Assume that you have two Python files, a.py and b.py. Now, a.py imports b.py. We run the a.py file, where the import b.py code is executed first. Before the rest of the a.py code runs, the code in the file b.py must run completely.
In the b.py code, there is some code that is exclusive to that file b.py and we don't want any other file (other than the b.py file), that has imported the b.py file, to run it.
So that is what this line of code checks. If it is the main file (i.e., b.py) running the code, which in this case it is not (a.py is the main file running), then only the code gets executed.

In python how __name__ resolves to __main__ by default? [duplicate]

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?.
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 then
it 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.
# Suppose this is foo.py.
print("before import")
import math
print("before function_a")
def function_a():
print("Function A")
print("before function_b")
def function_b():
print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
print("after __name__ guard")
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.
python foo.py
the interpreter will assign the hard-coded string "__main__" to the __name__ variable, i.e.
# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__"
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:
# Suppose this is in some other main program.
import foo
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.
# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"
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 called math. This is equivalent to replacing import math with the following (note that __import__ is a low-level function in Python that takes a string and triggers the actual import):
# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
It prints the string "before function_a".
It executes the def block, creating a function object, then assigning that function object to a variable called function_a.
It prints the string "before function_b".
It executes the second def block, creating another function object, then assigning it to a variable called function_b.
It prints the string "before __name__ guard".
Only When Your Module Is the Main Program
If your module is the main program, then it will see that __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
(instead) If your module is not the main program but was imported by another one, then __name__ will be "foo", not "__main__", and it'll skip the body of the if statement.
Always
It will print the string "after __name__ guard" in both situations.
Summary
In summary, here's what'd be printed in the two cases:
# What gets printed if foo is the main program
before import
before function_a
before function_b
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before function_a
before function_b
before __name__ guard
after __name__ guard
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 say python foo2.py on the command-line? Why?
# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo2 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
if __name__ == "__main__":
print("m1")
function_a()
print("m2")
print("t2")
Now, figure out what will happen if you remove the __name__ check in foo3.py:
# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo3 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
print("m1")
function_a()
print("m2")
print("t2")
What will this do when used as a script? When imported as a module?
# Suppose this is in foo4.py
__name__ = "__main__"
def bar():
print("bar")
print("before __name__ guard")
if __name__ == "__main__":
bar()
print("after __name__ guard")
When your script is run by passing it as a command to the Python interpreter,
python myscript.py
all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets run. Unlike other languages, there's no main() function that gets run automatically - the main() function is implicitly all the code at the top level.
In this case, the top-level code is an if block. __name__ is a built-in variable which evaluates to the name of the current module. However, if a module is being run directly (as in myscript.py above), then __name__ instead is set to the string "__main__". Thus, you can test whether your script is being run directly or being imported by something else by testing
if __name__ == "__main__":
...
If your script is being imported into another module, its various function and class definitions will be imported and its top-level code will be executed, but the code in the then-body of the if clause above won't get run as the condition is not met. As a basic example, consider the following two scripts:
# file one.py
def func():
print("func() in one.py")
print("top-level in one.py")
if __name__ == "__main__":
print("one.py is being run directly")
else:
print("one.py is being imported into another module")
# file two.py
import one
print("top-level in two.py")
one.func()
if __name__ == "__main__":
print("two.py is being run directly")
else:
print("two.py is being imported into another module")
Now, if you invoke the interpreter as
python one.py
The output will be
top-level in one.py
one.py is being run directly
If you run two.py instead:
python two.py
You get
top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly
Thus, when module one gets loaded, its __name__ equals "one" instead of "__main__".
Create the following two files:
# a.py
import b
# b.py
print("__name__ equals " + __name__)
if __name__ == '__main__':
print("if-statement was executed")
Now run each file individually.
Running python a.py:
$ python a.py
__name__ equals b
When a.py is executed, it imports the module b. This causes all the code inside b to run. Python sets globals()['__name__'] in the b module to the module's name, b.
Running python b.py:
$ python b.py
__name__ equals __main__
if-statement was executed
When only the file b.py is executed, Python sets globals()['__name__'] in this file to "__main__". Therefore, the if statement evaluates to True this time.
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?
Developing and Testing Your Code
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
The Problem
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.
A Better Way
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).
An Even Better Way
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()
if __name__ == "__main__" is the part that runs when the script is run from (say) the command line using a command like python myscript.py.
What does if __name__ == "__main__": do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
at the end of your mycode.py script,
when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
There are lots of different takes here on the mechanics of the code in question, the "How", but for me none of it made sense until I understood the "Why". This should be especially helpful for new programmers.
Take file "ab.py":
def a():
print('A function in ab file');
a()
And a second file "xy.py":
import ab
def main():
print('main function: this is where the action is')
def x():
print ('peripheral task: might be useful in other projects')
x()
if __name__ == "__main__":
main()
What is this code actually doing?
When you execute xy.py, you import ab. The import statement runs the module immediately on import, so ab's operations get executed before the remainder of xy's. Once finished with ab, it continues with xy.
The interpreter keeps track of which scripts are running with __name__. When you run a script - no matter what you've named it - the interpreter calls it "__main__", making it the master or 'home' script that gets returned to after running an external script.
Any other script that's called from this "__main__" script is assigned its filename as its __name__ (e.g., __name__ == "ab.py"). Hence, the line if __name__ == "__main__": is the interpreter's test to determine if it's interpreting/parsing the 'home' script that was initially executed, or if it's temporarily peeking into another (external) script. This gives the programmer flexibility to have the script behave differently if it's executed directly vs. called externally.
Let's step through the above code to understand what's happening, focusing first on the unindented lines and the order they appear in the scripts. Remember that function - or def - blocks don't do anything by themselves until they're called. What the interpreter might say if mumbled to itself:
Open xy.py as the 'home' file; call it "__main__" in the __name__ variable.
Import and open file with the __name__ == "ab.py".
Oh, a function. I'll remember that.
Ok, function a(); I just learned that. Printing 'A function in ab file'.
End of file; back to "__main__"!
Oh, a function. I'll remember that.
Another one.
Function x(); ok, printing 'peripheral task: might be useful in other projects'.
What's this? An if statement. Well, the condition has been met (the variable __name__ has been set to "__main__"), so I'll enter the main() function and print 'main function: this is where the action is'.
The bottom two lines mean: "If this is the "__main__" or 'home' script, execute the function called main()". That's why you'll see a def main(): block up top, which contains the main flow of the script's functionality.
Why implement this?
Remember what I said earlier about import statements? When you import a module it doesn't just 'recognize' it and wait for further instructions - it actually runs all the executable operations contained within the script. So, putting the meat of your script into the main() function effectively quarantines it, putting it in isolation so that it won't immediately run when imported by another script.
Again, there will be exceptions, but common practice is that main() doesn't usually get called externally. So you may be wondering one more thing: if we're not calling main(), why are we calling the script at all? It's because many people structure their scripts with standalone functions that are built to be run independent of the rest of the code in the file. They're then later called somewhere else in the body of the script. Which brings me to this:
But the code works without it
Yes, that's right. These separate functions can be called from an in-line script that's not contained inside a main() function. If you're accustomed (as I am, in my early learning stages of programming) to building in-line scripts that do exactly what you need, and you'll try to figure it out again if you ever need that operation again ... well, you're not used to this kind of internal structure to your code, because it's more complicated to build and it's not as intuitive to read.
But that's a script that probably can't have its functions called externally, because if it did it would immediately start calculating and assigning variables. And chances are if you're trying to re-use a function, your new script is related closely enough to the old one that there will be conflicting variables.
In splitting out independent functions, you gain the ability to re-use your previous work by calling them into another script. For example, "example.py" might import "xy.py" and call x(), making use of the 'x' function from "xy.py". (Maybe it's capitalizing the third word of a given text string; creating a NumPy array from a list of numbers and squaring them; or detrending a 3D surface. The possibilities are limitless.)
(As an aside, this question contains an answer by #kindall that finally helped me to understand - the why, not the how. Unfortunately it's been marked as a duplicate of this one, which I think is a mistake.)
The code under if __name__ == '__main__': will only be executed if the module is invoked as a script.
As an example, consider the following module my_test_module.py:
# my_test_module.py
print('This is going to be printed out, no matter what')
if __name__ == '__main__':
print('This is going to be printed out, only if user invokes the module as a script')
First possibility: Import my_test_module.py in another module
# main.py
import my_test_module
if __name__ == '__main__':
print('Hello from main.py')
Now if you invoke main.py:
python main.py
>> 'This is going to be printed out, no matter what'
>> 'Hello from main.py'
Note that only the top-level print() statement in my_test_module is executed.
Second possibility: Invoke my_test_module.py as a script
Now if you run my_test_module.py as a Python script, both print() statements will be executed:
python my_test_module.py
>>> 'This is going to be printed out, no matter what'
>>> 'This is going to be printed out, only if user invokes the module as a script'
For a more comprehensive explanation, you can read What does if __name__ == '__main__' do in Python.
When there are certain statements in our module (M.py) we want to be executed when it'll be running as main (not imported), we can place those statements (test-cases, print statements) under this if block.
As by default (when module running as main, not imported) the __name__ variable is set to "__main__", and when it'll be imported the __name__ variable will get a different value, most probably the name of the module ('M').
This is helpful in running different variants of a modules together, and separating their specific input & output statements and also if there are any test-cases.
In short, use this 'if __name__ == "main" ' block to prevent (certain) code from being run when the module is imported.
Put simply, __name__ is a variable defined for each script that defines whether the script is being run as the main module or it is being run as an imported module.
So if we have two scripts;
#script1.py
print "Script 1's name: {}".format(__name__)
and
#script2.py
import script1
print "Script 2's name: {}".format(__name__)
The output from executing script1 is
Script 1's name: __main__
And the output from executing script2 is:
Script1's name is script1
Script 2's name: __main__
As you can see, __name__ tells us which code is the 'main' module.
This is great, because you can just write code and not have to worry about structural issues like in C/C++, where, if a file does not implement a 'main' function then it cannot be compiled as an executable and if it does, it cannot then be used as a library.
Say you write a Python script that does something great and you implement a boatload of functions that are useful for other purposes. If I want to use them I can just import your script and use them without executing your program (given that your code only executes within the if __name__ == "__main__": context). Whereas in C/C++ you would have to portion out those pieces into a separate module that then includes the file. Picture the situation below;
The arrows are import links. For three modules each trying to include the previous modules code there are six files (nine, counting the implementation files) and five links. This makes it difficult to include other code into a C project unless it is compiled specifically as a library. Now picture it for Python:
You write a module, and if someone wants to use your code they just import it and the __name__ variable can help to separate the executable portion of the program from the library part.
To be short, you need to know several points:
import a action actually runs all that can be run in a.py, meaning each line in a.py
Because of point 1, you may not want everything to be run in a.py when importing it
To solve the problem in point 2, Python allows you to use a condition check
__name__ is an implicit variable in all .py modules:
when a.py is imported, the value of __name__ of a.py module is set to its file name "a"
when a.py is run directly using "python a.py", the value of __name__ is set to a string __main__
Based on the mechanism how Python sets the variable __name__ for each module, do you know how to achieve point 3? The answer is fairly easy, right? Use an if condition: if __name__ == "__main__": // do A
then python a.py will run the part // do A
and import a will skip the part // do A
You can even put if __name__ == "a" depending on your functional need, but rarely do
The important thing that Python is special at is point 4! The rest is just basic logic.
I've been reading so much throughout the answers on this page. I would say, if you know the thing, for sure you will understand those answers, otherwise, you are still confused.
Let's look at the answer in a more abstract way:
Suppose we have this code in x.py:
...
<Block A>
if __name__ == '__main__':
<Block B>
...
Blocks A and B are run when we are running x.py.
But just block A (and not B) is run when we are running another module, y.py for example, in which x.py is imported and the code is run from there (like when a function in x.py is called from y.py).
When you run Python interactively the local __name__ variable is assigned a value of __main__. Likewise, when you execute a Python module from the command line, rather than importing it into another module, its __name__ attribute is assigned a value of __main__, rather than the actual name of the module. In this way, modules can look at their own __name__ value to determine for themselves how they are being used, whether as support for another program or as the main application executed from the command line. Thus, the following idiom is quite common in Python modules:
if __name__ == '__main__':
# Do something appropriate here, like calling a
# main() function defined elsewhere in this module.
main()
else:
# Do nothing. This module has been imported by another
# module that wants to make use of the functions,
# classes and other useful bits it has defined.
Consider:
if __name__ == "__main__":
main()
It checks if the __name__ attribute of the Python script is "__main__". In other words, if the program itself is executed, the attribute will be __main__, so the program will be executed (in this case the main() function).
However, if your Python script is used by a module, any code outside of the if statement will be executed, so if __name__ == "__main__" is used just to check if the program is used as a module or not, and therefore decides whether to run the code.
Before explaining anything about if __name__ == '__main__' it is important to understand what __name__ is and what it does.
What is __name__?
__name__ is a DunderAlias - can be thought of as a global variable (accessible from modules) and works in a similar way to global.
It is a string (global as mentioned above) as indicated by type(__name__) (yielding <class 'str'>), and is an inbuilt standard for both Python 3 and Python 2 versions.
Where
It can not only be used in scripts but can also be found in both the interpreter and modules/packages.
Interpreter:
>>> print(__name__)
__main__
>>>
Script:
test_file.py:
print(__name__)
Resulting in __main__
Module or package:
somefile.py:
def somefunction():
print(__name__)
test_file.py:
import somefile
somefile.somefunction()
Resulting in somefile
Notice that when used in a package or module, __name__ takes the name of the file. The path of the actual module or package path is not given, but has its own DunderAlias __file__, that allows for this.
You should see that, where __name__, where it is the main file (or program) will always return __main__, and if it is a module/package, or anything that is running off some other Python script, will return the name of the file where it has originated from.
Practice
Being a variable means that it's value can be overwritten ("can" does not mean "should"), overwriting the value of __name__ will result in a lack of readability. So do not do it, for any reason. If you need a variable define a new variable.
It is always assumed that the value of __name__ to be __main__ or the name of the file. Once again changing this default value will cause more confusion that it will do good, causing problems further down the line.
Example:
>>> __name__ = 'Horrify' # Change default from __main__
>>> if __name__ == 'Horrify': print(__name__)
...
>>> else: print('Not Horrify')
...
Horrify
>>>
It is considered good practice in general to include the if __name__ == '__main__' in scripts.
Now to answer if __name__ == '__main__':
Now we know the behaviour of __name__ things become clearer:
An if is a flow control statement that contains the block of code will execute if the value given is true. We have seen that __name__ can take either
__main__ or the file name it has been imported from.
This means that if __name__ is equal to __main__ then the file must be the main file and must actually be running (or it is the interpreter), not a module or package imported into the script.
If indeed __name__ does take the value of __main__ then whatever is in that block of code will execute.
This tells us that if the file running is the main file (or you are running from the interpreter directly) then that condition must execute. If it is a package then it should not, and the value will not be __main__.
Modules
__name__ can also be used in modules to define the name of a module
Variants
It is also possible to do other, less common but useful things with __name__, some I will show here:
Executing only if the file is a module or package
if __name__ != '__main__':
# Do some useful things
Running one condition if the file is the main one and another if it is not
if __name__ == '__main__':
# Execute something
else:
# Do some useful things
You can also use it to provide runnable help functions/utilities on packages and modules without the elaborate use of libraries.
It also allows modules to be run from the command line as main scripts, which can be also very useful.
I think it's best to break the answer in depth and in simple words:
__name__: Every module in Python has a special attribute called __name__.
It is a built-in variable that returns the name of the module.
__main__: Like other programming languages, Python too has an execution entry point, i.e., main. '__main__' is the name of the scope in which top-level code executes. Basically you have two ways of using a Python module: Run it directly as a script, or import it. When a module is run as a script, its __name__ is set to __main__.
Thus, the value of the __name__ attribute is set to __main__ when the module is run as the main program. Otherwise the value of __name__ is set to contain the name of the module.
It is a special for when a Python file is called from the command line. This is typically used to call a "main()" function or execute other appropriate startup code, like commandline arguments handling for instance.
It could be written in several ways. Another is:
def some_function_for_instance_main():
dosomething()
__name__ == '__main__' and some_function_for_instance_main()
I am not saying you should use this in production code, but it serves to illustrate that there is nothing "magical" about if __name__ == '__main__'.
It just a convention for invoking a main function in Python files.
There are a number of variables that the system (Python interpreter) provides for source files (modules). You can get their values anytime you want, so, let us focus on the __name__ variable/attribute:
When Python loads a source code file, it executes all of the code found in it. (Note that it doesn't call all of the methods and functions defined in the file, but it does define them.)
Before the interpreter executes the source code file though, it defines a few special variables for that file; __name__ is one of those special variables that Python automatically defines for each source code file.
If Python is loading this source code file as the main program (i.e. the file you run), then it sets the special __name__ variable for this file to have a value "__main__".
If this is being imported from another module, __name__ will be set to that module's name.
So, in your example in part:
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
means that the code block:
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
will be executed only when you run the module directly; the code block will not execute if another module is calling/importing it because the value of __name__ will not equal to "main" in that particular instance.
Hope this helps out.
if __name__ == "__main__": is basically the top-level script environment, and it specifies the interpreter that ('I have the highest priority to be executed first').
'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.
if __name__ == "__main__":
# Execute only if run as a script
main()
Consider:
print __name__
The output for the above is __main__.
if __name__ == "__main__":
print "direct method"
The above statement is true and prints "direct method". Suppose if they imported this class in another class it doesn't print "direct method" because, while importing, it will set __name__ equal to "first model name".
In simple words:
The code you see under if __name__ == "__main__": will only get called upon when your Python file is executed as python example1.py
However, if you wish to import your Python file example1.py as a module to work with another Python file, say example2.py, the code under if __name__ == "__main__": will not run or take any effect.
You can make the file usable as a script as well as an importable module.
fibo.py (a module named fibo)
# Other modules can IMPORT this MODULE to use the function fib
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
Reference: https://docs.python.org/3.5/tutorial/modules.html
The reason for
if __name__ == "__main__":
main()
is primarily to avoid the import lock problems that would arise from having code directly imported. You want main() to run if your file was directly invoked (that's the __name__ == "__main__" case), but if your code was imported then the importer has to enter your code from the true main module to avoid import lock problems.
A side-effect is that you automatically sign on to a methodology that supports multiple entry points. You can run your program using main() as the entry point, but you don't have to. While setup.py expects main(), other tools use alternate entry points. For example, to run your file as a gunicorn process, you define an app() function instead of a main(). Just as with setup.py, gunicorn imports your code so you don't want it do do anything while it's being imported (because of the import lock issue).
If you are a beginner, probably the only answer you need right now is that this code is unnecessary for a simple script. It is only useful if you want to be able to import your script (or unpickle etc; see the other answers here for some other non-beginner scenarios).
In slightly different words, the if __name__ guard is a mechanism for hiding code from other code. If you don't have a specific reason to hide something, don't: If you don't need to hide some code from import, don't put it behind this guard, and if you do, hide as little as possible.
In slightly more detail, let's say you have a simple script fib.py (adapted from this answer):
# XXX FIXME: useless (see below)
if __name__ == "__main__":
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, if you simply run python fib.py it works fine. But __name__ will always be "__main__" in this scenario, so the condition is actually unnecessary. The script could be simplified to just
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, you can't import fib with the new version, but if you didn't plan to do that in the first place, this version is actually better, because it's simpler and clearer.
If you do want to be able to import fib, the first version is useless, too, because the useful code is in a section which will not run when you import this file (in which case __name__ will not be "__main__"). The proper design in that case would be to refactor the code so that the useful parts are in a function you can run when you want to after you have imported it.
def main():
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
if __name__ == "__main__":
main()
Now, if you import fib, the call to main() will not be executed; but when you run python fib.py, it will.
Actually, a better design still would be to isolate the reusable part (the actual calculation) from the user-visible input/output:
def fibn(n: int) -> int:
a, b = 0, 1
while b < n:
a, b = b, a+b
return b
def main() -> None:
n = int(input('Write a number: '))
print('Fibonacci number %i: %i' % (n, fibn(n)))
if __name__ == "__main__":
main()
Now, you can from fib import fibn and call the fibn() function from the code which performs this import.
(I called the function fibn() just to make it clearer what is what in this example. In real life, you might call it fib() and do from fib import fib.)
Similarly, you could import and call the main function if you wanted to reuse it.
Returning to the code in the question, I would similarly move the code from the if into a function as well, so that callers can invoke that function if they want to.
def main():
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
if __name__ == "__main__":
main()
This changes the scope of the lock variable; if the surrounding code needs access to it, you will need to make it global (or, perhaps, better, refactor main to return lock, and have the caller capture the value in a local variable of its own).
(Unlike in languages like C, the name main has no specific meaning to Python; but it's a common convention to use it as the name of the thing which will be run. You still have to actually explicitly call it, like main(), unlike in C.)
Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.
Small example to explain in short.
Script test.py
apple = 42
def hello_world():
print("I am inside hello_world")
if __name__ == "__main__":
print("Value of __name__ is: ", __name__)
print("Going to call hello_world")
hello_world()
We can execute this directly as
python test.py
Output
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
Now suppose we call the above script from another script:
Script external_calling.py
import test
print(test.apple)
test.hello_world()
print(test.__name__)
When you execute this,
python external_calling.py
Output
42
I am inside hello_world
test
So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.
This answer is for Java programmers learning Python.
Every Java file typically contains one public class. You can use that class in two ways:
Call the class from other files. You just have to import it in the calling program.
Run the class stand alone, for testing purposes.
For the latter case, the class should contain a public static void main() method. In Python this purpose is served by the globally defined label '__main__'.
If this .py file are imported by other .py files, the code under the if statement will not be executed.
If this .py are run by python this_py.py under shell, or double clicked in Windows. the code under the if statement will be executed.
It is usually written for testing.
We see if __name__ == '__main__': quite often.
It checks if a module is being imported or not.
In other words, the code within the if block will be executed only when the code runs directly. Here directly means not imported.
Let's see what it does using a simple code that prints the name of the module:
# test.py
def test():
print('test module name=%s' %(__name__))
if __name__ == '__main__':
print('call test()')
test()
If we run the code directly via python test.py, the module name is __main__:
call test()
test module name=__main__
If the Python interpreter is running a particular module then the __name__ global variable will have the value "__main__":
def a():
print("a")
def b():
print("b")
if __name__ == "__main__":
print ("you can see me")
a()
else:
print ("You can't see me")
b()
When you run this script, it prints:
you can see me
a
If you import this file, say A to file B, and execute the file B then if __name__ == "__main__" in file A becomes False, so it prints:
You can't see me
b
All the answers have pretty much explained the functionality. But I will provide one example of its usage which might help clearing out the concept further.
Assume that you have two Python files, a.py and b.py. Now, a.py imports b.py. We run the a.py file, where the import b.py code is executed first. Before the rest of the a.py code runs, the code in the file b.py must run completely.
In the b.py code, there is some code that is exclusive to that file b.py and we don't want any other file (other than the b.py file), that has imported the b.py file, to run it.
So that is what this line of code checks. If it is the main file (i.e., b.py) running the code, which in this case it is not (a.py is the main file running), then only the code gets executed.

How to read value of only one variable of a python script in another python script [duplicate]

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?.
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 then
it 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.
# Suppose this is foo.py.
print("before import")
import math
print("before function_a")
def function_a():
print("Function A")
print("before function_b")
def function_b():
print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
print("after __name__ guard")
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.
python foo.py
the interpreter will assign the hard-coded string "__main__" to the __name__ variable, i.e.
# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__"
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:
# Suppose this is in some other main program.
import foo
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.
# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"
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 called math. This is equivalent to replacing import math with the following (note that __import__ is a low-level function in Python that takes a string and triggers the actual import):
# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
It prints the string "before function_a".
It executes the def block, creating a function object, then assigning that function object to a variable called function_a.
It prints the string "before function_b".
It executes the second def block, creating another function object, then assigning it to a variable called function_b.
It prints the string "before __name__ guard".
Only When Your Module Is the Main Program
If your module is the main program, then it will see that __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
(instead) If your module is not the main program but was imported by another one, then __name__ will be "foo", not "__main__", and it'll skip the body of the if statement.
Always
It will print the string "after __name__ guard" in both situations.
Summary
In summary, here's what'd be printed in the two cases:
# What gets printed if foo is the main program
before import
before function_a
before function_b
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before function_a
before function_b
before __name__ guard
after __name__ guard
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 say python foo2.py on the command-line? Why?
# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo2 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
if __name__ == "__main__":
print("m1")
function_a()
print("m2")
print("t2")
Now, figure out what will happen if you remove the __name__ check in foo3.py:
# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo3 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
print("m1")
function_a()
print("m2")
print("t2")
What will this do when used as a script? When imported as a module?
# Suppose this is in foo4.py
__name__ = "__main__"
def bar():
print("bar")
print("before __name__ guard")
if __name__ == "__main__":
bar()
print("after __name__ guard")
When your script is run by passing it as a command to the Python interpreter,
python myscript.py
all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets run. Unlike other languages, there's no main() function that gets run automatically - the main() function is implicitly all the code at the top level.
In this case, the top-level code is an if block. __name__ is a built-in variable which evaluates to the name of the current module. However, if a module is being run directly (as in myscript.py above), then __name__ instead is set to the string "__main__". Thus, you can test whether your script is being run directly or being imported by something else by testing
if __name__ == "__main__":
...
If your script is being imported into another module, its various function and class definitions will be imported and its top-level code will be executed, but the code in the then-body of the if clause above won't get run as the condition is not met. As a basic example, consider the following two scripts:
# file one.py
def func():
print("func() in one.py")
print("top-level in one.py")
if __name__ == "__main__":
print("one.py is being run directly")
else:
print("one.py is being imported into another module")
# file two.py
import one
print("top-level in two.py")
one.func()
if __name__ == "__main__":
print("two.py is being run directly")
else:
print("two.py is being imported into another module")
Now, if you invoke the interpreter as
python one.py
The output will be
top-level in one.py
one.py is being run directly
If you run two.py instead:
python two.py
You get
top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly
Thus, when module one gets loaded, its __name__ equals "one" instead of "__main__".
Create the following two files:
# a.py
import b
# b.py
print("__name__ equals " + __name__)
if __name__ == '__main__':
print("if-statement was executed")
Now run each file individually.
Running python a.py:
$ python a.py
__name__ equals b
When a.py is executed, it imports the module b. This causes all the code inside b to run. Python sets globals()['__name__'] in the b module to the module's name, b.
Running python b.py:
$ python b.py
__name__ equals __main__
if-statement was executed
When only the file b.py is executed, Python sets globals()['__name__'] in this file to "__main__". Therefore, the if statement evaluates to True this time.
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?
Developing and Testing Your Code
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
The Problem
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.
A Better Way
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).
An Even Better Way
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()
if __name__ == "__main__" is the part that runs when the script is run from (say) the command line using a command like python myscript.py.
What does if __name__ == "__main__": do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
at the end of your mycode.py script,
when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
There are lots of different takes here on the mechanics of the code in question, the "How", but for me none of it made sense until I understood the "Why". This should be especially helpful for new programmers.
Take file "ab.py":
def a():
print('A function in ab file');
a()
And a second file "xy.py":
import ab
def main():
print('main function: this is where the action is')
def x():
print ('peripheral task: might be useful in other projects')
x()
if __name__ == "__main__":
main()
What is this code actually doing?
When you execute xy.py, you import ab. The import statement runs the module immediately on import, so ab's operations get executed before the remainder of xy's. Once finished with ab, it continues with xy.
The interpreter keeps track of which scripts are running with __name__. When you run a script - no matter what you've named it - the interpreter calls it "__main__", making it the master or 'home' script that gets returned to after running an external script.
Any other script that's called from this "__main__" script is assigned its filename as its __name__ (e.g., __name__ == "ab.py"). Hence, the line if __name__ == "__main__": is the interpreter's test to determine if it's interpreting/parsing the 'home' script that was initially executed, or if it's temporarily peeking into another (external) script. This gives the programmer flexibility to have the script behave differently if it's executed directly vs. called externally.
Let's step through the above code to understand what's happening, focusing first on the unindented lines and the order they appear in the scripts. Remember that function - or def - blocks don't do anything by themselves until they're called. What the interpreter might say if mumbled to itself:
Open xy.py as the 'home' file; call it "__main__" in the __name__ variable.
Import and open file with the __name__ == "ab.py".
Oh, a function. I'll remember that.
Ok, function a(); I just learned that. Printing 'A function in ab file'.
End of file; back to "__main__"!
Oh, a function. I'll remember that.
Another one.
Function x(); ok, printing 'peripheral task: might be useful in other projects'.
What's this? An if statement. Well, the condition has been met (the variable __name__ has been set to "__main__"), so I'll enter the main() function and print 'main function: this is where the action is'.
The bottom two lines mean: "If this is the "__main__" or 'home' script, execute the function called main()". That's why you'll see a def main(): block up top, which contains the main flow of the script's functionality.
Why implement this?
Remember what I said earlier about import statements? When you import a module it doesn't just 'recognize' it and wait for further instructions - it actually runs all the executable operations contained within the script. So, putting the meat of your script into the main() function effectively quarantines it, putting it in isolation so that it won't immediately run when imported by another script.
Again, there will be exceptions, but common practice is that main() doesn't usually get called externally. So you may be wondering one more thing: if we're not calling main(), why are we calling the script at all? It's because many people structure their scripts with standalone functions that are built to be run independent of the rest of the code in the file. They're then later called somewhere else in the body of the script. Which brings me to this:
But the code works without it
Yes, that's right. These separate functions can be called from an in-line script that's not contained inside a main() function. If you're accustomed (as I am, in my early learning stages of programming) to building in-line scripts that do exactly what you need, and you'll try to figure it out again if you ever need that operation again ... well, you're not used to this kind of internal structure to your code, because it's more complicated to build and it's not as intuitive to read.
But that's a script that probably can't have its functions called externally, because if it did it would immediately start calculating and assigning variables. And chances are if you're trying to re-use a function, your new script is related closely enough to the old one that there will be conflicting variables.
In splitting out independent functions, you gain the ability to re-use your previous work by calling them into another script. For example, "example.py" might import "xy.py" and call x(), making use of the 'x' function from "xy.py". (Maybe it's capitalizing the third word of a given text string; creating a NumPy array from a list of numbers and squaring them; or detrending a 3D surface. The possibilities are limitless.)
(As an aside, this question contains an answer by #kindall that finally helped me to understand - the why, not the how. Unfortunately it's been marked as a duplicate of this one, which I think is a mistake.)
The code under if __name__ == '__main__': will only be executed if the module is invoked as a script.
As an example, consider the following module my_test_module.py:
# my_test_module.py
print('This is going to be printed out, no matter what')
if __name__ == '__main__':
print('This is going to be printed out, only if user invokes the module as a script')
First possibility: Import my_test_module.py in another module
# main.py
import my_test_module
if __name__ == '__main__':
print('Hello from main.py')
Now if you invoke main.py:
python main.py
>> 'This is going to be printed out, no matter what'
>> 'Hello from main.py'
Note that only the top-level print() statement in my_test_module is executed.
Second possibility: Invoke my_test_module.py as a script
Now if you run my_test_module.py as a Python script, both print() statements will be executed:
python my_test_module.py
>>> 'This is going to be printed out, no matter what'
>>> 'This is going to be printed out, only if user invokes the module as a script'
For a more comprehensive explanation, you can read What does if __name__ == '__main__' do in Python.
When there are certain statements in our module (M.py) we want to be executed when it'll be running as main (not imported), we can place those statements (test-cases, print statements) under this if block.
As by default (when module running as main, not imported) the __name__ variable is set to "__main__", and when it'll be imported the __name__ variable will get a different value, most probably the name of the module ('M').
This is helpful in running different variants of a modules together, and separating their specific input & output statements and also if there are any test-cases.
In short, use this 'if __name__ == "main" ' block to prevent (certain) code from being run when the module is imported.
Put simply, __name__ is a variable defined for each script that defines whether the script is being run as the main module or it is being run as an imported module.
So if we have two scripts;
#script1.py
print "Script 1's name: {}".format(__name__)
and
#script2.py
import script1
print "Script 2's name: {}".format(__name__)
The output from executing script1 is
Script 1's name: __main__
And the output from executing script2 is:
Script1's name is script1
Script 2's name: __main__
As you can see, __name__ tells us which code is the 'main' module.
This is great, because you can just write code and not have to worry about structural issues like in C/C++, where, if a file does not implement a 'main' function then it cannot be compiled as an executable and if it does, it cannot then be used as a library.
Say you write a Python script that does something great and you implement a boatload of functions that are useful for other purposes. If I want to use them I can just import your script and use them without executing your program (given that your code only executes within the if __name__ == "__main__": context). Whereas in C/C++ you would have to portion out those pieces into a separate module that then includes the file. Picture the situation below;
The arrows are import links. For three modules each trying to include the previous modules code there are six files (nine, counting the implementation files) and five links. This makes it difficult to include other code into a C project unless it is compiled specifically as a library. Now picture it for Python:
You write a module, and if someone wants to use your code they just import it and the __name__ variable can help to separate the executable portion of the program from the library part.
To be short, you need to know several points:
import a action actually runs all that can be run in a.py, meaning each line in a.py
Because of point 1, you may not want everything to be run in a.py when importing it
To solve the problem in point 2, Python allows you to use a condition check
__name__ is an implicit variable in all .py modules:
when a.py is imported, the value of __name__ of a.py module is set to its file name "a"
when a.py is run directly using "python a.py", the value of __name__ is set to a string __main__
Based on the mechanism how Python sets the variable __name__ for each module, do you know how to achieve point 3? The answer is fairly easy, right? Use an if condition: if __name__ == "__main__": // do A
then python a.py will run the part // do A
and import a will skip the part // do A
You can even put if __name__ == "a" depending on your functional need, but rarely do
The important thing that Python is special at is point 4! The rest is just basic logic.
I've been reading so much throughout the answers on this page. I would say, if you know the thing, for sure you will understand those answers, otherwise, you are still confused.
Let's look at the answer in a more abstract way:
Suppose we have this code in x.py:
...
<Block A>
if __name__ == '__main__':
<Block B>
...
Blocks A and B are run when we are running x.py.
But just block A (and not B) is run when we are running another module, y.py for example, in which x.py is imported and the code is run from there (like when a function in x.py is called from y.py).
When you run Python interactively the local __name__ variable is assigned a value of __main__. Likewise, when you execute a Python module from the command line, rather than importing it into another module, its __name__ attribute is assigned a value of __main__, rather than the actual name of the module. In this way, modules can look at their own __name__ value to determine for themselves how they are being used, whether as support for another program or as the main application executed from the command line. Thus, the following idiom is quite common in Python modules:
if __name__ == '__main__':
# Do something appropriate here, like calling a
# main() function defined elsewhere in this module.
main()
else:
# Do nothing. This module has been imported by another
# module that wants to make use of the functions,
# classes and other useful bits it has defined.
Consider:
if __name__ == "__main__":
main()
It checks if the __name__ attribute of the Python script is "__main__". In other words, if the program itself is executed, the attribute will be __main__, so the program will be executed (in this case the main() function).
However, if your Python script is used by a module, any code outside of the if statement will be executed, so if __name__ == "__main__" is used just to check if the program is used as a module or not, and therefore decides whether to run the code.
Before explaining anything about if __name__ == '__main__' it is important to understand what __name__ is and what it does.
What is __name__?
__name__ is a DunderAlias - can be thought of as a global variable (accessible from modules) and works in a similar way to global.
It is a string (global as mentioned above) as indicated by type(__name__) (yielding <class 'str'>), and is an inbuilt standard for both Python 3 and Python 2 versions.
Where
It can not only be used in scripts but can also be found in both the interpreter and modules/packages.
Interpreter:
>>> print(__name__)
__main__
>>>
Script:
test_file.py:
print(__name__)
Resulting in __main__
Module or package:
somefile.py:
def somefunction():
print(__name__)
test_file.py:
import somefile
somefile.somefunction()
Resulting in somefile
Notice that when used in a package or module, __name__ takes the name of the file. The path of the actual module or package path is not given, but has its own DunderAlias __file__, that allows for this.
You should see that, where __name__, where it is the main file (or program) will always return __main__, and if it is a module/package, or anything that is running off some other Python script, will return the name of the file where it has originated from.
Practice
Being a variable means that it's value can be overwritten ("can" does not mean "should"), overwriting the value of __name__ will result in a lack of readability. So do not do it, for any reason. If you need a variable define a new variable.
It is always assumed that the value of __name__ to be __main__ or the name of the file. Once again changing this default value will cause more confusion that it will do good, causing problems further down the line.
Example:
>>> __name__ = 'Horrify' # Change default from __main__
>>> if __name__ == 'Horrify': print(__name__)
...
>>> else: print('Not Horrify')
...
Horrify
>>>
It is considered good practice in general to include the if __name__ == '__main__' in scripts.
Now to answer if __name__ == '__main__':
Now we know the behaviour of __name__ things become clearer:
An if is a flow control statement that contains the block of code will execute if the value given is true. We have seen that __name__ can take either
__main__ or the file name it has been imported from.
This means that if __name__ is equal to __main__ then the file must be the main file and must actually be running (or it is the interpreter), not a module or package imported into the script.
If indeed __name__ does take the value of __main__ then whatever is in that block of code will execute.
This tells us that if the file running is the main file (or you are running from the interpreter directly) then that condition must execute. If it is a package then it should not, and the value will not be __main__.
Modules
__name__ can also be used in modules to define the name of a module
Variants
It is also possible to do other, less common but useful things with __name__, some I will show here:
Executing only if the file is a module or package
if __name__ != '__main__':
# Do some useful things
Running one condition if the file is the main one and another if it is not
if __name__ == '__main__':
# Execute something
else:
# Do some useful things
You can also use it to provide runnable help functions/utilities on packages and modules without the elaborate use of libraries.
It also allows modules to be run from the command line as main scripts, which can be also very useful.
I think it's best to break the answer in depth and in simple words:
__name__: Every module in Python has a special attribute called __name__.
It is a built-in variable that returns the name of the module.
__main__: Like other programming languages, Python too has an execution entry point, i.e., main. '__main__' is the name of the scope in which top-level code executes. Basically you have two ways of using a Python module: Run it directly as a script, or import it. When a module is run as a script, its __name__ is set to __main__.
Thus, the value of the __name__ attribute is set to __main__ when the module is run as the main program. Otherwise the value of __name__ is set to contain the name of the module.
It is a special for when a Python file is called from the command line. This is typically used to call a "main()" function or execute other appropriate startup code, like commandline arguments handling for instance.
It could be written in several ways. Another is:
def some_function_for_instance_main():
dosomething()
__name__ == '__main__' and some_function_for_instance_main()
I am not saying you should use this in production code, but it serves to illustrate that there is nothing "magical" about if __name__ == '__main__'.
It just a convention for invoking a main function in Python files.
There are a number of variables that the system (Python interpreter) provides for source files (modules). You can get their values anytime you want, so, let us focus on the __name__ variable/attribute:
When Python loads a source code file, it executes all of the code found in it. (Note that it doesn't call all of the methods and functions defined in the file, but it does define them.)
Before the interpreter executes the source code file though, it defines a few special variables for that file; __name__ is one of those special variables that Python automatically defines for each source code file.
If Python is loading this source code file as the main program (i.e. the file you run), then it sets the special __name__ variable for this file to have a value "__main__".
If this is being imported from another module, __name__ will be set to that module's name.
So, in your example in part:
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
means that the code block:
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
will be executed only when you run the module directly; the code block will not execute if another module is calling/importing it because the value of __name__ will not equal to "main" in that particular instance.
Hope this helps out.
if __name__ == "__main__": is basically the top-level script environment, and it specifies the interpreter that ('I have the highest priority to be executed first').
'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.
if __name__ == "__main__":
# Execute only if run as a script
main()
Consider:
print __name__
The output for the above is __main__.
if __name__ == "__main__":
print "direct method"
The above statement is true and prints "direct method". Suppose if they imported this class in another class it doesn't print "direct method" because, while importing, it will set __name__ equal to "first model name".
In simple words:
The code you see under if __name__ == "__main__": will only get called upon when your Python file is executed as python example1.py
However, if you wish to import your Python file example1.py as a module to work with another Python file, say example2.py, the code under if __name__ == "__main__": will not run or take any effect.
You can make the file usable as a script as well as an importable module.
fibo.py (a module named fibo)
# Other modules can IMPORT this MODULE to use the function fib
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
Reference: https://docs.python.org/3.5/tutorial/modules.html
The reason for
if __name__ == "__main__":
main()
is primarily to avoid the import lock problems that would arise from having code directly imported. You want main() to run if your file was directly invoked (that's the __name__ == "__main__" case), but if your code was imported then the importer has to enter your code from the true main module to avoid import lock problems.
A side-effect is that you automatically sign on to a methodology that supports multiple entry points. You can run your program using main() as the entry point, but you don't have to. While setup.py expects main(), other tools use alternate entry points. For example, to run your file as a gunicorn process, you define an app() function instead of a main(). Just as with setup.py, gunicorn imports your code so you don't want it do do anything while it's being imported (because of the import lock issue).
If you are a beginner, probably the only answer you need right now is that this code is unnecessary for a simple script. It is only useful if you want to be able to import your script (or unpickle etc; see the other answers here for some other non-beginner scenarios).
In slightly different words, the if __name__ guard is a mechanism for hiding code from other code. If you don't have a specific reason to hide something, don't: If you don't need to hide some code from import, don't put it behind this guard, and if you do, hide as little as possible.
In slightly more detail, let's say you have a simple script fib.py (adapted from this answer):
# XXX FIXME: useless (see below)
if __name__ == "__main__":
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, if you simply run python fib.py it works fine. But __name__ will always be "__main__" in this scenario, so the condition is actually unnecessary. The script could be simplified to just
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
Now, you can't import fib with the new version, but if you didn't plan to do that in the first place, this version is actually better, because it's simpler and clearer.
If you do want to be able to import fib, the first version is useless, too, because the useful code is in a section which will not run when you import this file (in which case __name__ will not be "__main__"). The proper design in that case would be to refactor the code so that the useful parts are in a function you can run when you want to after you have imported it.
def main():
n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))
if __name__ == "__main__":
main()
Now, if you import fib, the call to main() will not be executed; but when you run python fib.py, it will.
Actually, a better design still would be to isolate the reusable part (the actual calculation) from the user-visible input/output:
def fibn(n: int) -> int:
a, b = 0, 1
while b < n:
a, b = b, a+b
return b
def main() -> None:
n = int(input('Write a number: '))
print('Fibonacci number %i: %i' % (n, fibn(n)))
if __name__ == "__main__":
main()
Now, you can from fib import fibn and call the fibn() function from the code which performs this import.
(I called the function fibn() just to make it clearer what is what in this example. In real life, you might call it fib() and do from fib import fib.)
Similarly, you could import and call the main function if you wanted to reuse it.
Returning to the code in the question, I would similarly move the code from the if into a function as well, so that callers can invoke that function if they want to.
def main():
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
if __name__ == "__main__":
main()
This changes the scope of the lock variable; if the surrounding code needs access to it, you will need to make it global (or, perhaps, better, refactor main to return lock, and have the caller capture the value in a local variable of its own).
(Unlike in languages like C, the name main has no specific meaning to Python; but it's a common convention to use it as the name of the thing which will be run. You still have to actually explicitly call it, like main(), unlike in C.)
Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.
Small example to explain in short.
Script test.py
apple = 42
def hello_world():
print("I am inside hello_world")
if __name__ == "__main__":
print("Value of __name__ is: ", __name__)
print("Going to call hello_world")
hello_world()
We can execute this directly as
python test.py
Output
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
Now suppose we call the above script from another script:
Script external_calling.py
import test
print(test.apple)
test.hello_world()
print(test.__name__)
When you execute this,
python external_calling.py
Output
42
I am inside hello_world
test
So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.
This answer is for Java programmers learning Python.
Every Java file typically contains one public class. You can use that class in two ways:
Call the class from other files. You just have to import it in the calling program.
Run the class stand alone, for testing purposes.
For the latter case, the class should contain a public static void main() method. In Python this purpose is served by the globally defined label '__main__'.
If this .py file are imported by other .py files, the code under the if statement will not be executed.
If this .py are run by python this_py.py under shell, or double clicked in Windows. the code under the if statement will be executed.
It is usually written for testing.
We see if __name__ == '__main__': quite often.
It checks if a module is being imported or not.
In other words, the code within the if block will be executed only when the code runs directly. Here directly means not imported.
Let's see what it does using a simple code that prints the name of the module:
# test.py
def test():
print('test module name=%s' %(__name__))
if __name__ == '__main__':
print('call test()')
test()
If we run the code directly via python test.py, the module name is __main__:
call test()
test module name=__main__
If the Python interpreter is running a particular module then the __name__ global variable will have the value "__main__":
def a():
print("a")
def b():
print("b")
if __name__ == "__main__":
print ("you can see me")
a()
else:
print ("You can't see me")
b()
When you run this script, it prints:
you can see me
a
If you import this file, say A to file B, and execute the file B then if __name__ == "__main__" in file A becomes False, so it prints:
You can't see me
b
All the answers have pretty much explained the functionality. But I will provide one example of its usage which might help clearing out the concept further.
Assume that you have two Python files, a.py and b.py. Now, a.py imports b.py. We run the a.py file, where the import b.py code is executed first. Before the rest of the a.py code runs, the code in the file b.py must run completely.
In the b.py code, there is some code that is exclusive to that file b.py and we don't want any other file (other than the b.py file), that has imported the b.py file, to run it.
So that is what this line of code checks. If it is the main file (i.e., b.py) running the code, which in this case it is not (a.py is the main file running), then only the code gets executed.

Categories