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.
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__: bHere, 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: cThis 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: cTo 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: cIn summary, the if __name__ == '__main__' construct is essential for controlling script execution and avoiding accidental code runs when modules are imported.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
