[Python] What does if __name__ == “__main__”: do?
When your script is run by passing it as a command to the Python interpreter,
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
__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
The output will be
top-level in one.py one.py is being run directly
If you run
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
"one" instead of
What does the
if __name__ == "__main__": do?
# Threading example import time, thread def myfunction(string, sleeptime, lock, *args): while True: lock.acquire() time.sleep(sleeptime) lock.release() time.sleep(sleeptime) 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))
You can make the file usable as a script as well as an importable module.
fibo.py (a module named
# 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))
Before explaining anything about
if __name__ == '__main__' it is important to understand what
__name__ is and what it does.
__name__ is a DunderAlias - can be thought of as a global variable (accessible from modules) and works in a similar way to
It can not only be used in scripts but can also be found in both the interpreter and modules/packages.
>>> print(__name__) __main__ >>>
Module or package:
def somefunction(): print(__name__)
import somefile somefile.somefunction()
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.
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.
>>> __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:
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.
__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
__name__ can also be used in modules to define the name of a module
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.
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.
if name == 'main':
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
Let's see what it does using a simple code that prints the name of the modue:
# 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
call test() test module name=__main__
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.
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
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
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__ == "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
- 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
- Oh, a function. I'll remember that.
- Another one.
x(); ok, printing 'peripheral task: might be useful in other projects'.
- What's this? An
ifstatement. 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 3rd 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 thread 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.]
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
ifblock 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:
and running it (on a command prompt) with something like:
~$ python important.py
However, if you want to import the module to another script:
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
__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
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 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 basically Top-level script environment, 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()
if __name__ == "__main__": main()
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
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.
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
As the only special case, however, in whatever Python process you run, as in mycode.py:
the otherwise anonymous global namespace is assigned the value of
'__main__' to its
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()