Fundamentals 6 min read

Understanding the __name__ Variable in Python Scripts

This article explains how the __name__ variable is set when running Python scripts directly or importing them, demonstrates its behavior with multiple .py files, shows how unintended code can be executed, and introduces the if __name__ == '__main__' guard to control execution.

IT Services Circle
IT Services Circle
IT Services Circle
Understanding the __name__ Variable in Python Scripts

The article introduces the special __name__ variable in Python and shows that when a script is executed directly (e.g., python a.py ), __name__ is automatically set to __main__ .

Example with a single file a.py :

# a.py
print(__name__)    # python a.py
# __main__

When the same script is run, the output is __main__ . The article then presents a second file b.py with identical content, confirming the same behavior.

# b.py
print(__name__)    # python b.py
# __main__

Next, the article demonstrates how __name__ differs when a module is imported. Two files, a.py and b.py , are created:

# b.py
def hello_b():
    print('hello_b __name__:', __name__)
# a.py
from b import hello_b
hello_b()
# Output when running python a.py:
# hello_b __name__: b

Here, a.py is the entry point, so its __name__ is __main__ , while the imported b.py sees __name__ as 'b' , not __main__ .

The article expands the example to three files ( a.py , b.py , c.py ) to illustrate that each imported module receives its own filename as __name__ :

# b.py
def hello_b():
    print('from b.py:', __name__)
# c.py
def hello_c():
    print('from c.py:', __name__)
# a.py
from b import hello_b
from c import hello_c
hello_b()  # from b.py: b
hello_c()  # from c.py: c

This explains why the printed output shows from b.py: b and from c.py: c .

The article then warns about accidental execution of unwanted code when imported modules contain top‑level statements, such as a print statement for a public account ID. Running python a.py prints the unwanted line because it resides in b.py and is executed on import.

# b.py
def hello_b():
    print('from b.py:', __name__)
print('@public account: IT Service Circle')
# c.py
def hello_c():
    print('from c.py:', __name__)
# a.py
from b import hello_b
from c import hello_c
hello_b()
hello_c()
# Output:
# @public account: IT Service Circle
# from b.py: b
# from c.py: c

To prevent such side effects, the article introduces the conventional guard:

if __name__ == '__main__':
    # code that should run only when the file is executed directly
    print('@public account: IT Service Circle')

By placing the print statement inside this block in b.py , running python a.py no longer prints the unwanted line, and only the intended module outputs appear:

# from b.py: b
# from c.py: c

In summary, the if __name__ == '__main__' construct is essential for controlling script execution and avoiding accidental code runs when modules are imported.

pythonprogrammingscript@Importmain__name__conditional
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.