What does if __name__ == "__main__": do?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.