Python Understanding __name__ == ‘__main__’

python 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.

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.

 

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.

 

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

Now if we run test.py we will see that it prints out “myImport” because that module is set to print its name every time it is run regardless if its from an import or not.

 

Seeing it in Action.

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

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

and run test.py we will get the following output

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

we see that we get the following output.

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 help demonstrate how the “if __name__ ==  ‘__main__’ ” statement works and how it can be valuable to utilize in your modules. Let me know if there are any questions or if this was able to help! If you would like to browse my other Python related articles you can find a collection of them here.

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*