What does if __name__ == “__main__”: do?

What does if __name__ == “__main__”: do? The best answer I found after searching for it in Google was in StackOverFlow. According to it, whenever the Python interpreter reads a source file, it does two things:

  1. it sets a few special variables like __name__, and then
  2. it executes all of the code found in the file.

In my previous post What the F*** is F-string in Python, many viewers asked me about sharing on this topic. I learned it from several sources and here is what I came to know.

From the Official Documentation

More official documentation can be found here. Here, it states that '__main__' is the name of the scope in which top-level code executes.

A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

A module can discover whether or not it is running in the main scope by checking its own __name__, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -mbut not when it is imported:

if __name__ == "__main__":
    # execute only if run as a script
    main()

Simplifying it!

To simplify the above statements:

Whenever a Python script is run, sets some special variables. __name__ is one of them.

To understand this, let us trying printing what value is assigned to this special variable. Say you have a script named name.py

print("My name is {}".format(__name__))

Output

My name is __main__

You create another name and import name.py as a module. Let us name this script second_name.py.

import name

Output

My name is name

Here the variable __name__ is no longer __main__ because the script second_name.py is no longer directly interpreted by the Python interpreter but by a module (named name).

Now, let us add another line of instruction to second_name.py.

import name

print("My second name is {}".format(__name__))

Output

My name is name
My second name is __main__

The second line is interpreted directly by Python. Therefore, __name__ is assigned __main__.

Getting into if __name__ == “__main__”:

Now, let me show you why sometimes if __name__ == “__main__”: statement is required.

Change your name.py with the following block of code:

def main():
    print("My name is {}".format(__name__))
if __name__ == "__main__": 
    main()

Output

My name is __main__

Now re-run your second_name.py script.

import name

print("My second name is {}".format(__name__))

Output

My second name is __main__

Here in second_name.py first line does not get executed. This is because of the module name.py contains the statement if __name__ == “__main__”:. The statement makes it clear that it will get executed only if it is directly interpreted by Python. But here is executed by the module name.py. Thus, the first line does not print the output. 

To understand further about it, you may try adding else statement in name.py as:

if __name__ == "__main__": 
    #some instructions
else:
    #some instructions

Then see how second_name.py reacts based on the changes you make in name.py. 

Summing Up

From this piece, you can conclude that the statement if __name__ == “__main__”: is a conditional statement which is used to check whether a module in the Python program is directly run or being imported. 

You may also check the section “You may also like to read …”  below for such posts.

Leave a Comment

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

Scroll to Top