What is __name__ == ‘__main__’ in Python?

Python Tutorials

Summary: We often see and use __name__ == '__main__' expression in Python scripts or modules. what is its significance, why it is used in Python? Lets’ discuss this in this article.

Python interpreter before running any Python script or module set few variables for the file. One such variable is __name__.

By default, the value of the __name__ variable is the name of the file (i.e. name of the file followed by .py) but when we run a python module as the main program, the interpreter set it as __main__.

Python files with .py extensions are also known as Python modules.

Let’s see some examples for better understanding.

Example 1: Running a Single Python File

The first.py file has the following fun1 function that outputs the value of __name__:

def fun1():
    print("The name of first.py file is: ",__name__)

fun1()

If we run the code as python first.py, we get the value of __name__ variable as __main__ .

The name of first.py file is: __main__

This clearly states that the python interpreter is modifying the value of the __name__ variable to __main__.

The Python interpreter does this when it reads a module as a script, or when a module is invoked from an interactive prompt (using python -m).

But does this happen to every module imported into our Python program?

Let’s see another example to get the answer.

Example 2: Importing and Running a Python File

Consider the following two python files with respective codes for instance.

first.py:

import second

def fun1():
    print("The name of first.py file is: ",__name__)

fun1()

second.py:

def fun2():
    print("The name of second.py file is: ",__name__)

fun2()

Both the python modules have a function that outputs the value of the __name__ variable, and as an addition, the first.py file imports the second.py.

If we run the first.py file as python first.py, we observe that the Python interpreter changes only the __name__ of the first.py to __main__.

The value of __name__ of the second.py remains the same.

The name of second.py file is: second
The name of first.py file is: __main__

It is so because we have run the first.py directly whereas indirectly imported the second.py file.

When and How to use the __name__ variable?

We often write Python modules that we want to run directly instead of being imported into another program.

Take the previous example for the idea, we don’t want the fun2 of the second.py to execute when it is imported.

In such cases, we can use the __name__ variable with the if clause to prevent a module from running.

Let’s rewrite the last example with if __name__ == '__main__'.

Example 3: Prevent Imported Module from Executing using if __name__ == ‘__main__’:

first.py:

import second

def fun1():
    print("The name of first.py file is: ",__name__)

if __name__ == '__main__':
    fun1()

second.py:

def fun2():
    print("The name of second.py file is: ",__name__)

if __name__ == '__main__':
    fun2()

This time when we run the first.py using python first.py, we observe that the fun2 of the second.py doesn’t get executed.

The name of first.py file is: __main__

This is because the value of __name__ in second.py is not set to '__main__' by the interpreter (since it is imported), due to which the __name__ == '__main__' result into false.

Example 4: Execute all modules in a Single Python File

Since we have imported the second.py into first.py, we can execute the fun2 in the first.py as follows.

first.py:

import second

def fun1():
    print("The name of first.py file is: ",__name__)

if __name__ == '__main__':
    fun1()
    second.fun2()

second.py:

def fun2():
    print("The name of second.py file is: ",__name__)

if __name__ == '__main__':
    fun2()

If we execute the first.py using the python first.py command, we get the output of both the functions.

The name of first.py file is: __main__
The name of second.py file is: second

Conclusion

If we directly invoke a module as a script (i.e. run it from an interactive prompt), the Python interpreter set the __name__ property of that module to '__main__'.

The value of __name__ of the imported modules remains the same as their module’s name.

We can use the __name__ == '__main__' expression as a condition in if clause to prevent the module from being executed when it is imported rather than getting executed directly.

Leave a Reply

Your email address will not be published. Required fields are marked *