Get AI summaries of any video or article — Sign up free
Python Tutorial: if __name__ == '__main__' thumbnail

Python Tutorial: if __name__ == '__main__'

Corey Schafer·
4 min read

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.

TL;DR

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?

Python assigns `__name__` before executing the module’s code. If `first_module.py` is run directly, `__name__` becomes `'__main__'`. If `second_module.py` imports `first_module`, then `first_module.__name__` becomes `'first_module'` (the module’s filename without `.py`). That difference is what drives the `if __name__ == '__main__':` check.

Why does importing a module often cause unexpected prints or side effects without the `__name__` guard?

Top-level code in a module runs whenever the module is imported. If printing, starting a process, or running a script-like sequence sits at the top level, it will execute during import as well. Wrapping script-only behavior in `if __name__ == '__main__':` ensures that importers get functions and variables without triggering the module’s “main” routine.

How does the `else` branch help verify the behavior?

Adding an `else` makes the runtime path visible. Running `first_module.py` directly triggers the `'__main__'` branch (e.g., printing “run directly”). Importing it from `second_module.py` triggers the `else` branch (e.g., printing “run from import”), because `__name__` is `'first_module'`, not `'__main__'`.

What’s the practical difference between code placed inside vs. outside `if __name__ == '__main__':`?

Code outside the conditional runs every time the module is imported or executed directly. Code inside the conditional runs only when the module is the direct entry point. In the tutorial’s example, a “this will always be run” print placed outside the block appears even when `first_module` is imported, while “first module main method” placed inside runs only on direct execution.

If importing doesn’t run the conditional block, how can another module still trigger that logic?

Another module can call the function explicitly. For example, `second_module.py` can run `first_module.main()` after importing `first_module`. That bypasses the `__name__` check because the call is made directly, so the “main method” prints and the rest of the logic proceed.

Review Questions

  1. If `second_module.py` imports `first_module`, what is the expected value of `first_module.__name__`, and which branch of `if __name__ == '__main__':` runs?
  2. Where should script-only code be placed to avoid side effects during import, and why?
  3. How can `second_module.py` trigger `first_module`’s main logic even when the `__name__` conditional doesn’t run on import?

Key Points

  1. 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. 2

    `if __name__ == '__main__':` is the standard switch for running code only when the module is the entry point.

  3. 3

    Top-level statements in a module execute on import, so they should be used only for import-safe setup or definitions.

  4. 4

    Code inside the `if __name__ == '__main__':` block runs only during direct execution, preventing unwanted side effects for importers.

  5. 5

    An `else` branch can be used to confirm whether the module is running directly or being imported.

  6. 6

    Another module can still call the guarded logic explicitly (e.g., `first_module.main()`) after importing, if that behavior is desired.

Highlights

`__name__` becomes `'__main__'` only when the module is run directly; importing changes it to the module’s actual name.
The conditional prevents “main script” behavior—like prints or one-off routines—from firing during import.
Anything outside the `if __name__ == '__main__':` block runs every time the module is imported.
Even with the guard, other modules can invoke the main logic directly by calling a function such as `first_module.main()`.

Topics