If you are new to Python, you might have noticed that it’s 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 not self-explanatory especially for newcomers.
Python __name__ Explained
In Python, __name__
is a special variable assigned to the name of the Python module by the interpreter. If your module is invoked as a script, then the string ‘__main__’
will automatically be assigned to the special variable __name__
. But if you import your module into another module, the string ‘my_module’
will be assigned to __name__
.
In today’s tutorial, we’ll explore and discuss the purpose of a main method and what to expect when you define one in your Python applications.
What Is the Purpose of __name__ in Python?
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 the 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 into 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')
In the module above, we have one print statement that’s outside of the main method and another print statement that’s inside. The code under the main method will only be executed if the module is invoked as a script from, for example 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_module
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.py
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’ve imported it into second_module the main method in first_module will simply be ignored because if __name__ == ‘__main__'
evaluates to False
. Recall that __name__
variable for second_module
has been assigned string '__main__'
while the first_module
__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’s 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()
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.
Understanding If__Name__== ‘__main__’ in Python
In this article, I have described how the main method gets executed in Python and under what conditions. When a module is invoked as a string, then Python interpreter will assign the string '__main__'
to a special variable called __name__
, and the code, which is defined under the condition if __name__ == ‘__main__'
, will subsequently get executed.
On the other hand, when a module is imported in another module, then the Python interpreter will assign the string with the name of that module to the special variable __name__
. This means that in such cases if __name__ == ‘__main__’
will evaluate to False
which means that only the code outside of this condition will get executed once imported.