What Does if __name__ == '__main__' Mean?
Learn what if __name__ == '__main__' means in Python, how __name__ works on run vs import, and why the main guard keeps modules reusable, with examples.
Expected Interview Answer
The check if __name__ == '__main__' lets a Python file run code only when it is executed directly, not when it is imported as a module. Python sets the special variable __name__ to '__main__' for the file you run, and to the module's name when it is imported elsewhere.
Every Python module has a __name__ attribute. When you run a script directly, the interpreter sets that module's __name__ to the string '__main__'. When the same file is imported, __name__ becomes the module's own name instead. Guarding startup code behind this condition keeps that code from firing on import, which is essential for reusable modules, test discovery, and multiprocessing on some platforms.
- Separates reusable definitions from run-only startup code
- Prevents side effects when a module is imported
- Makes a file usable both as a script and as a library
- Supports clean unit testing and tooling that imports the module
- Required for safe multiprocessing entry points on Windows
AI Mentor Explanation
Think of a player who trains drills privately but only walks out to bat when it is their own match. If another team merely borrows their coaching notes, the player should not suddenly march onto the pitch. The main guard is that rule: run the innings only when this is your match, stay in the pavilion when someone else just imports your techniques.
Step-by-Step Explanation
Step 1
Know __name__
Every module has a __name__ attribute set automatically by the interpreter.
Step 2
Direct run
When you execute a file with python file.py, its __name__ becomes the string '__main__'.
Step 3
Imported run
When the file is imported, __name__ is set to the module's name, not '__main__'.
Step 4
Add the guard
Wrap startup code in if __name__ == '__main__': so it runs only on direct execution.
Step 5
Call a main() function
Put logic in a main() function and call it inside the guard for clean, testable structure.
What Interviewer Expects
- Correct explanation that __name__ is '__main__' only on direct run
- Understanding that imports set __name__ to the module name
- Why the guard prevents unwanted side effects on import
- Ability to structure code with a main() function
- Awareness of its role in testing and multiprocessing
Common Mistakes
- Saying it is required for every Python file to run
- Believing code inside the guard runs when the module is imported
- Confusing __name__ with the file name string
- Putting no reusable code outside the guard, making the module unusable as a library
- Misspelling the dunder as _name_ or main without underscores
Best Answer (HR Friendly)
“It is a simple check that tells Python to run certain code only when the file is launched directly, and to skip it when the file is imported into another program. This lets one file work both as a runnable script and as a reusable library.”
Code Example
def greet(name):
return f'Hello, {name}!'
def main():
# Startup code that should run only on direct execution
print(greet('World'))
print('__name__ is:', __name__)
if __name__ == '__main__':
main()
# If this file is imported, greet() is available
# but main() does NOT run automatically.Follow-up Questions
- What value does __name__ hold when a module is imported?
- Why is the guard important for the multiprocessing module?
- How does this pattern help with unit testing?
- What is a dunder (double underscore) variable in Python?
- Can you have multiple entry points in one file using this idiom?
MCQ Practice
1. What is __name__ set to when a file is run directly?
Python sets __name__ to the string '__main__' for the module you execute directly.
2. When you import a module, its __name__ becomes?
On import, __name__ holds the module's name, so the main guard's block is skipped.
3. Why wrap startup code in if __name__ == '__main__':?
The guard ensures startup logic runs only on direct execution, not when the file is imported.
Flash Cards
What is __name__ on direct run? — The string '__main__' — set by the interpreter for the file you execute.
What is __name__ when imported? — The module's own name, so the main-guard block does not run.
Purpose of the main guard? — Run startup code only when the file is executed directly, not when imported.
Why does multiprocessing need it? — On spawn-based platforms child processes re-import the module; the guard stops re-running startup code.