Python: __name__ == “__main__” Explained

You see it everywhere.

If you have been coding in Python for a while, or just casually browsing Python Github repositories, you probably have come across this snippet of code.

if __name__ == '__main__':
  # do something

It is ubiquitous.

In fact, this is how many times this above snippet appears in Github!

18 millions+ times!

So without further ado, let’s study what this snippet of code does exactly.

What is __name__?

__name__ is simply a built-in variable in Python which evaluates to the name of the current module.

To fully understand what __name__ is and how it used, let’s go through a series of examples.

Example 1

Let’s create a simple Python script in a file called test.py

# test.py
print("__name__ in test.py is set to " + __name__)

What this line does is that it is going to print out the variable __name__ on the screen so that we can examine it.

Let’s run this script from the terminal and see what we get.

$ python test.py

Here is the output that you will see on the screen.

$ python3 test.py
__name__ in test.py is set to __main__

From this, we can conclude that in this example, the variable __name__ was set to the string value __main__

Now let’s look at a slightly different example.

Example 2

Let’s create a new file test2.py in the same directory as test.py

In this new file, let’s import test so that we can examine the __name__ variable in test.py and let’s also print the __name__ variable in test2.py

# test2.py
import test

print("__name__ in test2.py is set to " + __name__)

If you run the test2.py script from the terminal, this is what you will see on your screen.

$ python3 test2.py
__name__ in test.py is set to test
__name__ in test2.py is set to __main__

Hmm interesting, so what is going on?

Basically, what’s happening is that __name__  is set at the module level.

It is set to the name of the module. In other words, for every module in your code, __name__ will be set to that module name.

And for the execution entry point, Python’s main script, the __name__ variable will be set to __main__

That’s cool and all but how is this special variable used in practice?

How is __name__ used in real applications?

One reason for doing this is that if you want to write a module that can be executed directly or alternatively be imported and used in another module.

For example, let’s create a dummy module hello.py that others can import to their own scripts, or they could directly execute it if they wish to.

Here is what this module is supposed to do.

If you directly execute the module, then it should print Hello, Afternerd on the screen.

But if you import it instead, it sould provide you a function hello() where you can control who to greet.

# hello.py
def hello(name):
  print(f"Hello, {name}!")

if __name__ == '__main__':
  hello('Afternerd')

Now let’s create a main script main.py that will import the hello module.

# main.py
from hello import hello

hello('Karim')

What do you think will happen if we directly execute hello.py?

We just get Hello, Afternerd! printed on the screen since the if condition inside the hello module will be satisfied!

$ python3 hello.py
Hello, Afternerd!

How about if we directly execute main.py instead?

Now, we will only get Hello, Karim! printed on the screen.

$ python3 main.py
Hello, Karim!

But what if we hadn’t included the if __name__ == ‘main’ condition in the hello module?

If you hadn’t included the if condition in your module, you would get this undesired output when you run the main script.

$ python3 main.py
Hello, Afternerd!
Hello, Karim!

Learning Python?

Check out the Courses section!

Featured Posts

Are you Beginning your Programming Career?

I provide my best content for beginners in the newsletter.

  • Python tips for beginners, intermediate, and advanced levels.
  • CS Career tips and advice.
  • Special discounts on my premium courses when they launch.

And so much more…

Subscribe now. It’s Free.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments