What does if __name__ == “__main__” do?

As an Amazon Associate I earn from qualifying purchases.

3 Min Read

The short answer

What does if __name__ == “__main__” do? Having if __name__ == "__main__" in a Python script will allow any code in this if statement to execute if that Python script is executed as the main script. If the Python script is imported into another script and the other script is executed, it will not be run.

Explanation

Let’s say you have 2 scripts: script1.py and script2.py and they have the following code:

# script1.py

import script2
# script2.py

print("Hello, I am script2.py")

if __name__ == '__main__':
    print ("Hello World from %s!" % __name__)

If you run script2.py you will get the following output:

$ python script2.py
Hello, I am script2.py
Hello World from __main__!

Since script2.py was executed as the main Python script, both print statements are executed and __name__ is set to __main__.

So what happens when we run script1.py?

$ python script1.py
Hello, I am script2.py

In this case, only Hello, I am script2.py is printed. This is because script2.py was not the main script executed.

Why do we need if __name__ == “_main__” ?

When you write a Python script, your intentions for the script can be one of 3 things: to execute as a standalone file, to execute from another python script as a module, or both! if __name__ == "__main__" gives you the ability to change the behavior of the script given the context of its use.

i.e you could decide that you want a script to behave differently depending on if it is the main script or if it is imported.

How does __name__ get set?

__name__ is a special variable that is set by the Python interpreter and gets set to the name of the module. However, if the module is run directly, __name__ for that particular module is set to __main__.

Example

Let’s say I have the following 2 scripts:

script1.py

# script1.py

import script2

print ("Hello World from %s!" % __name__)

script2.py

# script2.py

print("Hello, I am %s!" % __name__)

If I run script2.py, __name__ in this file is set to __main__ since this the executed script:

$ python script2.py
Hello, I am __main__!

But what happens when we run script1.py?

$ python script1.py
Hello, I am script2!
Hello World from __main__!

__name__ in script2.py now becomes script2. This is because its is not the executed script, rather imported.

__name__ in script1.py gets set to __main__

Conclusion

We have answered the questions ‘What does if name == “main” do?’, ‘Why do we need if __name__ == “_main__” ?’ and ‘How does __name__ get set?’

  • Having if __name__ == "__main__" in a Python script will allow any code in this if statement to execute if that Python script is executed as the main script. If the Python script is imported into another script and the other script is executed, it will not be run
  • if __name__ == "__main__" gives you the ability to change the behavior of the script given the context of its use.
  • __name__ is a special variable that is set by the Python interpreter and gets set to the name of the module. However, if the module is run directly, __name__ for that particular module is set to __main__

Need more help with Python? Check out these other tutorials.

If you’re interested in learning Python I highly recommend this book. In the first half of the book, you”ll learn basic programming concepts, such as variables, lists, classes, and loops, and practice writing clean code with exercises for each topic. In the second half, you”ll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, a set of data visualizations with Python”s handy libraries, and a simple web app you can deploy online. Get it here.

Leave a Comment

Your email address will not be published.

Subscribe to my newsletter to keep up to date with my latest posts

Holler Box