What does if __name__ == ”__main__” do?
By: Giorgos Myrianthous
Tags: Python Programming, Software Engineering, Coding, Software Development, Python

What does if __name__ == ”__main__” do?

When and how a main method is executed in Python

Photo by Blake Connally on unsplash.com

If you are new to Python, you might have noticed that it is possible to run a Python script with or without a main method. And the notation used in Python to define one (i.e. if __name__ == ‘__main__' ) is definitely not self-explanatory especially for new comers.

In this article, I am going to explore what is the purpose of a main method and what to expect when you define one in your Python applications.

What is the purpose of __name__ ?

Before executing a program, the Python Interpreter assigns the name of the python module into a special variable called __name__ . Depending on whether you are executing the program through command line or importing the module into another module, the assignment for __name__ will vary.

If you invoke your module as a script, for instance

python my_module.py

then Python Interpreter will automatically assign the string '__main__' to the special variable __name__ . On the other hand, if your module is imported in another module

# Assume that this is another_module.py

import my_module

then the string 'my_module' will be assigned to __name__ .

How does the main method work?

Now let’s assume that we have the following module, that contains the following lines of code:

# first_module.py print('Hello from first_module.py') if __name__ == '__main__':

print('Hello from main method of first_module.py')

So in the module above, we have one print statement which is outside of the main method and one more print statement which is inside. The code under the main method, will only be executed if the module is invoked as a script from (e.g.) the command line, as shown below:

python first_module.py

Hello from first_module.py

Hello from main method of first_module.py

Now, let’s say that instead of invoking module first_module as a script, we want to import it in another module:

# second_module.py import first_script print('Hello from second_module.py') if __name__ == '__main__':

print('Hello from main method of second_module.py')

And finally, we invoke second_module as a script:

python second_module

Hello from first_module.py

Hello from second_module.py

Hello from main method of second_module.py

Notice that the first output comes from module first_module and specifically from the print statement which is outside the main method. Since we haven’t invoked first_module as a script but instead we have imported it into second_module the main method in first_module will be simply ignored since if __name__ == ‘__main__' evaluates to False . Recall that from the above call, __name__ variable for second_module has been assigned string '__main__' while the first_module ‘s __name__ variable has been assigned the name of the module, i.e. ’first_module’ .

Although everything under if __name__ == ‘__main__' is considered to be what we call a “main method”, it is a good practice to define one proper main method instead, which is called if the condition evaluates to True. For instance,

# my_module.py def main():

"""The main function of my Python Application"""

print('Hello World') if __name__ == '__main__':

main()

Note: I would generally discourage you from having multiple main functions in a single Python application. I have used two different main methods just for the sake of the example.

Conclusion