Post Image

Understanding __name__ == '__main__'

If you have been looking at any amount of Python code online and you have almost surly seen the following code at the end of a file.

# classes and/or functions 

if __name__ == '__main__': 
    
    # other code here

When first seeing that if statement it can be confusing as to why it is in there and what benefit it gives and what it does but once after reading this article you may find yourself adding this into your modules all the time.

 

What does it do?

The if statement if __name__ == '__main__': is a way of controlling the execution of some code so that it is only run when the module is called directly and not if the module is called via an import. This removes the need to have a dedicated launcher script that starts your program and instead you can just do it from one of your existing modules directly such as your packets __init__.py file.

 

What does it do?

The if statement if __name__ == '__main__': is a way of controlling the execution of some code so that it is only run when the module is called directly and not if the module is called via an import. This removes the need to have a dedicated launcher script that starts your program and instead you can just do it from one of your existing modules directly.

 

How does it work?

The __name__ variable is a special variable that is automatically created by the Python interpreter when a script is run. When you tell python to run a file such as python3 myscript.py during the execution of the script the myscript.py module will be referred to globally as ‘__main__’. Looking at the if statement we can see that its checking if the variable __name__ is set to the string “__main__” and if it is it executes the code.

 

If the Module is Imported What is its __name__?

If we have a file “test.py” that only contains the import statement below.

import myImport

And we were to make a second module called “myImport.py” and had it contain the following code.

#!/usr/bin/env python3
 
print(__name__)

Now if we run test.py we will see that it prints out “myImport” because the print statement is running because it was imported. If we run myImport.py directly it will print out __main__. This demonstrates that the exact same piece of code run in 2 different contexts will have that variable be different.

 

Seeing it in Action.

With our 2 files test.py and myImport.py lets modify the myImport.py file to contain the following code.

#!/usr/bin/env python3
 
def my_function():
    print('this is my function')
 
if __name__ == '__main__':
    print(__name__)
    my_function()

Looking at this code we can see because the definition of the function “my_function” is not after the “if __name__” statement the function will be defined but not run.

Then the code after the if __name__ statement will only be run if the module is run directly but not run if it is imported by another module.

and we change test.py to have the following code

import myImport
 
print('Now the myImport module is not printing out its __name__')
print('But we get access to its function "my_function", see')
myImport.my_function()

and run test.py we will get the following output

Now the myImport module is not printing out its __name__
But we get access to its function "my_function", see
this is my function

As expected there is no __name__ variable printed out because our test.py module does not have the code to do so and it is not printed from the myImport.py module because the call to print the __name__ variable is after the “if __name__” block.

 

Now lets run the myImport module directly

python3 myImport.py

we see that we get the following output.

__main__
this is my function

This shows that we still get access to all the modules in the upper section of our module as they are defined prior to the code under the “if __name__” statement.

 

I hope this article was able to better clear up what if __name__ == '__main__' does and how it is useful in your daily programming.



Comments (0)
Leave a Comment