name
main
python

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.

Browse interview questions

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?

  1. 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.
  2. 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__":?

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

python
1# my_module.py
2
3def greet():
4    print("Hello, world!")
5
6if __name__ == "__main__":
7    greet()
  • Running Directly:
bash
  python my_module.py

Output:

 
Hello, world!

When my_module.py is run directly, __name__ is set to "__main__", so greet() is called.

  • Importing as a Module:
python
1  # another_script.py
2
3  import my_module
4
5  my_module.greet()

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

  1. Running Tests:
    • You can include test code under if __name__ == "__main__": to verify that the module's functions work as expected when run directly.
  2. 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.