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

print("__name__ in 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

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

$ python3
__name__ in 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 in the same directory as

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

import test

print("__name__ in is set to " + __name__)

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

$ python3
__name__ in is set to test
__name__ in 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 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.

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

if __name__ == '__main__':

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

from hello import hello


What do you think will happen if we directly execute

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

$ python3
Hello, Afternerd!

How about if we directly execute instead?

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

$ python3
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
Hello, Afternerd!
Hello, Karim!

Learning Python?

If you are a beginner, then I highly recommend this book.

No longer a beginner?

Then it’s time to take your Python skills to the next level with this book (It’s my favorite).

Featured Posts

Python: A Learning Path from Zero to Hero

The Ultimate Path for Learning Computer Science

Pass your Coding Interview like a Boss

A Roadmap for Learning Git

Why (and How) you should Start your Programming Blog Today?

Are you Beginning your Programming Career?

I provide my best content for beginners in the newsletter.

  • What programming language to start with?
  • Do you need a CS degree to be a programmer?
  • Career tips and advice
  • Programming tutorials

And so much more…

Subscribe now. It’s Free.

2019-01-13T03:04:38+00:00 By |

Leave A Comment