Python Tutorial: if __name__ == '__main__'
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Python sets `__name__` to `'__main__'` when a file is executed directly, and to the module’s name (e.g., `'first_module'`) when imported.
Briefing
The line `if __name__ == '__main__':` is a gatekeeper in Python that decides whether a file is being executed directly or merely imported, letting developers run “main” code only in the direct-run case. That distinction matters because importing a module should typically reuse its functions and variables without triggering side effects like printing, starting servers, or running one-off scripts.
When Python starts running a `.py` file, it sets special variables before any of the file’s code executes. One of those variables is `__name__`. If the file is launched directly (for example, running `first_module.py` as a script), Python assigns `__name__` the value `'__main__'`. In contrast, when the same file is imported from another module (for example, `second_module.py` imports `first_module`), Python sets `__name__` to the module’s actual name—`'first_module'` in this case.
Core behavior becomes clear through the tutorial’s experiments. Running `first_module.py` directly prints that `first_module`’s `__name__` is `'__main__'`, so the conditional passes and the code inside the `if __name__ == '__main__':` block runs. Then, importing `first_module` from `second_module.py` changes the outcome: `first_module.__name__` becomes `'first_module'`, so the conditional fails and the “main method” code does not execute during import.
To make the difference even more explicit, the tutorial adds an `else` branch. When `first_module.py` is executed directly, the “run directly” path triggers; when it’s imported, the “run from import” path triggers instead. This pattern is the practical reason the conditional exists: it supports modules that can both (1) act as reusable libraries when imported and (2) behave like standalone scripts when run directly.
The tutorial also highlights a common best practice: placing code that should always run outside the `if __name__ == '__main__':` block, while keeping code meant only for direct execution inside it. Anything at the top level of the module executes on import, so prints or setup logic placed outside the conditional will happen every time. Meanwhile, code inside the conditional runs only when the module is the entry point.
Finally, the tutorial shows how to call the “main” logic explicitly from another module. Even if importing doesn’t trigger the conditional, `second_module.py` can still invoke `first_module.main()` directly if it wants that behavior. The result is a clean separation between import-time behavior and script entry-point behavior, using `__name__` as the switch.
Cornell Notes
`__name__` is a special Python variable that tells whether a module is running as the entry point or being imported. When a file is executed directly, Python sets `__name__` to `'__main__'`, so code under `if __name__ == '__main__':` runs. When the file is imported, Python sets `__name__` to the module’s name (e.g., `'first_module'`), so the conditional block is skipped. This prevents side effects during import while still allowing the same file to act like a runnable script. Code outside the conditional runs on every import, while code inside runs only for direct execution.
What values can `__name__` take, and when does each occur?
Why does importing a module often cause unexpected prints or side effects without the `__name__` guard?
How does the `else` branch help verify the behavior?
What’s the practical difference between code placed inside vs. outside `if __name__ == '__main__':`?
If importing doesn’t run the conditional block, how can another module still trigger that logic?
Review Questions
- If `second_module.py` imports `first_module`, what is the expected value of `first_module.__name__`, and which branch of `if __name__ == '__main__':` runs?
- Where should script-only code be placed to avoid side effects during import, and why?
- How can `second_module.py` trigger `first_module`’s main logic even when the `__name__` conditional doesn’t run on import?
Key Points
- 1
Python sets `__name__` to `'__main__'` when a file is executed directly, and to the module’s name (e.g., `'first_module'`) when imported.
- 2
`if __name__ == '__main__':` is the standard switch for running code only when the module is the entry point.
- 3
Top-level statements in a module execute on import, so they should be used only for import-safe setup or definitions.
- 4
Code inside the `if __name__ == '__main__':` block runs only during direct execution, preventing unwanted side effects for importers.
- 5
An `else` branch can be used to confirm whether the module is running directly or being imported.
- 6
Another module can still call the guarded logic explicitly (e.g., `first_module.main()`) after importing, if that behavior is desired.