The Python __name__ Variable

Introduction

The Python __name__ variable (with a double underscore before and after “name”) is a special built-in variable that holds the name of the current imported module. When accessed within a Python script file that’s executed directly (i.e. not a module), its value is the string "__main__".

__name__ is most often used to differentiate between Python code that is run in a standalone script or code that’s imported as a module. This feature is particularly useful for letting Python files that operate as modules, execute code that only runs as when used as. standalone scripts. Examples for such code include initialization tasks, testing functions, demo code, command-line interface logic, or setup code.

Accessing the __name__ Variable

As the introduction mentioned, when a Python script executes, Python sets the __name__ variable to "__main__" in the script that is being executed directly. However, if the script is imported as a module into another script, the __name__ variable is set to the name of the module. __name__ is available anywhere, and all you need to do to access its value is type __name__.

Let’s see this in action with an example that includes two Python files. The first file is called my_module.py and the second my_script.py, acting as a module and the main executable script, respectively.

Example:

# my_module.py

def get_standard_greeting():
   return "Hello"
   
print(f"Hello from my_module.py.  The value of __name__  \"{__name__}\".")
# my_script.py

import my_module

print(f"Hello from my_script.py.  The value of __name__  \"{__name__}\".")

To generate the output, we’ll run my_script.py at the terminal with the following command:

$ python my_script.py

Output:

Hello from my_module.py.  The value of __name__  "my_module".
Hello from my_script.py.  The value of __name__  "__main__".

In the output above, we first see the printout from when my_module is imported. It’s generated by the print statement in my_module.py (line 6). The value of __name__ there is the string "my_module", which is the name of the module. On the second output line, we see the printout from the execution of the main script. This time, it’s generated by the print statement in my_script.py (line 5). The value of __name__ there is the string "__main__", as is expected for the directly executed main script.

Now let’s run my_module.py. We can run the module file since any Python module is also a regular Python file that can be executed. This is the command we use:

$ python my_module.py

Output:

Hello from my_module.py.  The value of __name__  "__main__".

When we run the module, the output is just one line because my_script.py is not involved. The printout is being generated by the print statement in my_module.py (line 5). And since the module file is run directly now, the value of __name__ is __main__.

Running Code Conditionally by the Value of __name__

Because the value of __name__ is different when a module is run as a script and when imported, it’s possible to test for its value with an if statement and execute some code only when it’s one or the other.

Example:

# my_module.py

def get_standard_greeting():
   return "Hello"
   
if __name__ == "__main__":
    print(f"The standard greeting is {get_standard_greeting()}.")

The output below is generated by running my_module.py directly.

Output:

The standard greeting is Hello.

In the above example, the code tests the value of __name__ with an if statement (line 6) and executes the code block when it’s” “__main__", i.e. only when the file is run directly as a script and not when imported as a module. The message prints (line 7) when the module file runs, but not when the module is imported.

Summary & Reference for Using the __name__ Variable in Python

__name__ is a special variable set by Python. When a script runs directly, __name__ is set to the string “__main__“, whereas when imported, it takes on the name of the module.


This feature is useful for letting Python files execute code depending on whether they run as standalone scripts or imported as modules.

# module file

def get_standard_greeting():
   return "Hello"
   
if __name__ == "__main__":
    print(f"The standard greeting is {get_standard_greeting()}.")