PY-101 ยท Modules and packages
if __name__ == "__main__"
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Every Python file has a built-in variable __name__. It is "__main__" when you run the file directly, and the module's name when another file imports it. Putting start-up code under if __name__ == "__main__": means it runs only when the file is run, not when it is imported.
- Know what __name__ holds
- Guard start-up code
- Make a file both a module and a script
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
def greet(name):return f"Hello, {name}!"def main():print(greet("Asha"))print("Running as:", __name__)if __name__ == "__main__":main()
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Hello, Asha!
Running as: __main__ ๐ฏ Move the print into a main() function and call it only under if __name__ == "__main__":. Output stays: Total: 60
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
def main(): print("Total:", add_all([10, 20, 30])), then the guard calls main().
- The problemYou write useful functions in tools.py with a quick demo at the bottom. A colleague imports tools.py and the demo suddenly runs inside their program. The guard stops that.
- The Python idea
def main():Every Python file has a built-in variable __name__. - In real lifeCommand-line tools put argument parsing in main(), so their functions can also be imported and tested without running the tool.
Start with a question
You write useful functions in tools.py with a quick demo at the bottom. A colleague imports tools.py and the demo suddenly runs inside their program. The guard stops that.
Every reusable Python script uses this pattern. You will see it at the bottom of almost every file.
Explain it like I'm 10
It is a note on the door: "only do the welcome show if you came in through the front door, not if someone carried you in".
The technical explanation
Run python tools.py and inside it __name__ == "__main__".
import tools from another file sets tools.__name__ to "tools", so the guarded block is skipped.
In this editor your code runs as the main program, so the guarded block runs.
Syntax
def main():
...
if __name__ == "__main__":
main() Real-world scenario
Command-line tools put argument parsing in main(), so their functions can also be imported and tested without running the tool.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Classes and objects โQuestions people ask
Is if __name__ == "__main__" required?
No. A script works without it. It matters when the file might be imported, which is most files that grow beyond a few lines.
What does dunder mean in Python?
"Double underscore", the names like __name__ and __init__ that Python uses for special meaning.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Is if __name__ == "__main__" required?
No. A script works without it. It matters when the file might be imported, which is most files that grow beyond a few lines.
beginner What does dunder mean in Python?
"Double underscore", the names like __name__ and __init__ that Python uses for special meaning.
intermediate What is __name__ when you run a file directly?
"__main__". Directly run files are __main__.
intermediate What is __name__ when the file is imported as tools?
"tools". It is the module name.
Quick revision
Remember
- Know what __name__ holds
- Guard start-up code
- Make a file both a module and a script
- Put the guarded code in a main() function and call main() under the guard. It keeps the global scope clean and makes main testable.
Syntax
def main():
...
if __name__ == "__main__":
main() Official documentation
- __main__ โ top-level code (docs.python.org)