Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is __name__ in Python

Understanding __name__ in Python

When you start learning Python, you come across many double-underscore (dunder) attributes and methods. They might seem cryptic at first, but each one has a special meaning. Among these, the __name__ attribute is one of the most intriguing yet essential concepts for any Python programmer to grasp. Let's explore what __name__ is and how it's used in Python.

The Basics of __name__

In Python, __name__ is a built-in variable. It's not something you need to declare or set explicitly; Python does that for you. This variable is a string that holds the name of the module in which it is used. If you're wondering what a module is, it's simply a file containing Python code - a script or a part of your program.

__name__ in a Python Script

Let's start with a simple example. Create a Python file named and write the following code:

print("Hello, world!")
print("__name__ value:", __name__)

When you run this script directly, the output will be:

Hello, world!
__name__ value: __main__

Here, __name__ has the value '__main__'. This tells us that is not being imported from another module but is being run as the main program.

__name__ in Imported Modules

Now, let's see what happens when we import this script into another module. Create another file named and write the following code:

import hello

print("Greet says hello!")
print("__name__ value in greet:", __name__)

Run, and you'll see something like this:

Hello, world!
__name__ value: hello
Greet says hello!
__name__ value in greet: __main__

Notice that when is imported into, the __name__ value in is 'hello', which is the name of the module. In, __name__ is still '__main__', because is the script being executed.

The if __name__ == "__main__": Check

This is where the magic happens. If you want certain code to run only when the script is executed directly and not when it's imported, you can use an if check with __name__.

Modify like this:

def main():
    print("Hello, world!")

if __name__ == "__main__":

Now, when you run directly, it will print "Hello, world!" as expected. However, if you import into, nothing will be printed from because the main() function will only be called if is the main module.

Analogies to Help Understand __name__

Think of a Python script as a book. When you read the book directly, you're the "main" reader, and you're going through the content from start to finish. That's like running the script directly, where __name__ is '__main__'.

However, if someone references a chapter of your book in their own book, they are importing your content. Your chapter is no longer the "main" content; it's part of something larger. That's like importing a module, where __name__ is the name of the module (or chapter) being imported.

Practical Uses of __name__

Testing Code

One practical use of __name__ is to write tests within the same file as the code being tested. You can include a test function and call it only if the file is run directly. This way, you can test your code without running tests when the module is imported.

Making Modules Executable

Sometimes, you might want to make a module executable, providing a user interface or a command-line interface. The if __name__ == "__main__": check allows you to add this functionality without affecting how the module behaves when imported.

Organizing Code

Using __name__ to control the execution of code helps in organizing scripts. You can have definitions and implementations in the same file while controlling what gets executed when the file is imported or run directly.


In your journey as a Python developer, understanding the __name__ variable is like discovering the secret ingredient in a recipe. It's not just a quirky part of the language; it's a powerful tool that, when used correctly, can give your code the flexibility and organization it needs to be both reusable and executable.

As you continue to code and build more complex programs, you'll find that __name__ is a friend that helps keep your modules polite. They know when to speak up (when run directly) and when to stay quiet (when being imported). So next time you write a Python script, remember the special role of __name__, and use it to your advantage to craft clean, efficient, and well-behaved code. Happy coding!