What does if __name__ == "__main__": do?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Python, the construct if __name__ == "__main__": is used to determine if a script is being run directly or being imported as a module into another script. Here's a detailed explanation:
What Does if __name__ == "__main__": Do?
- Special Variable
__name__:- Every Python module has a special built-in variable called
__name__. - When a module is run directly,
__name__is set to"__main__". - When a module is imported into another module,
__name__is set to the module's name.
- Purpose of
if __name__ == "__main__"::- This construct checks whether the script is being run directly.
- If the script is being run directly, the block of code under this condition will execute.
- If the script is being imported, the block of code under this condition will not execute.
Why Use if __name__ == "__main__":?
- Script vs. Module:
- It allows a Python file to be used both as a reusable module and as a standalone script.
- This is useful for testing or running a script independently while still allowing it to be imported without running its main code.
- Organizing Code:
- It helps in organizing code better by keeping the script's main functionality within this conditional block.
- Functions and classes can be defined at the top level of the module, and the script's executable part can be placed under
if __name__ == "__main__":.
Example
Here's an example to illustrate this:
- Running Directly:
Output:
When my_module.py is run directly, __name__ is set to "__main__", so greet() is called.
- Importing as a Module:
When my_module.py is imported into another_script.py, __name__ in my_module is set to "my_module". Therefore, the greet() function is not called automatically.
Practical Use Cases
- Running Tests:
- You can include test code under
if __name__ == "__main__":to verify that the module's functions work as expected when run directly.
- Scripts with Main Functionality:
- For scripts that perform a series of actions (like data processing, running simulations, etc.), you can place the main execution code under this conditional block.
Conclusion
The construct if __name__ == "__main__": is a common Python idiom that helps you write flexible and reusable code. It allows you to run code only when a script is executed directly, providing a clear entry point for script execution while avoiding unintended code execution when the module is imported elsewhere.
Related reading
- What does the "yield" keyword do in Python?
- What algorithm does Python's built-in sort method use?
- What alternative is there to execfile in Python 3? / How to include a Python file?
- What am I missing from this csv reader for TensorFlow?
- What am I missing in python-multiprocessing/multithreading?
- What and When to use Tuple?
- What are data classes and how are they different from common classes?
- What are Flask Blueprints, exactly?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.