What does if __name__ == "__main__": do?

What Does if __name__ == "__main__": Do in Python?

In Python, the construct if __name__ == "__main__": is used to ensure that certain code is executed only when the script is run directly, and not when it is imported as a module in another script. This is a common practice in Python programming to control the execution of code blocks.

How It Works

Every Python module has a special built-in attribute called __name__. When a Python script is run directly, the __name__ attribute is set to "__main__". However, when the script is imported as a module in another script, the __name__ attribute is set to the module's name.

Example

Here is a simple example to demonstrate how it works:

my_script.py

def main(): print("This is the main function.") if __name__ == "__main__": main()

When you run my_script.py directly:

python my_script.py

Output:

This is the main function.

When you import my_script in another script:

another_script.py

import my_script

Running another_script.py:

python another_script.py

Output:

(no output)

In this case, my_script.py is imported as a module, so the main() function is not executed because the condition if __name__ == "__main__": is false.

Why Use if __name__ == "__main__":

  1. Prevent Unintended Execution:

    • Ensures that code intended to run only when the script is executed directly does not run when the script is imported as a module in another script.
  2. Reusability:

    • Allows the script to be imported and reused as a module without executing the code block within the if __name__ == "__main__": condition.
  3. Testing:

    • Facilitates testing by allowing you to write test code within the if __name__ == "__main__": block, ensuring it only runs during direct execution.

Practical Use Case

Consider a script that includes functions and a testing block:

example.py

def add(a, b): return a + b def subtract(a, b): return a - b if __name__ == "__main__": # This code block will only run when the script is executed directly. print("Testing add function:") print(add(5, 3)) # Output: 8 print("Testing subtract function:") print(subtract(5, 3)) # Output: 2

When you run example.py directly, the test code runs. However, when you import example in another script, the test code does not run, allowing you to use the add and subtract functions without executing the test code.

Conclusion

The if __name__ == "__main__": construct is a valuable feature in Python that allows you to control the execution of code blocks. It ensures that certain code runs only when the script is executed directly, and not when it is imported as a module. This practice enhances code reusability, prevents unintended execution, and facilitates testing.

For a deeper understanding of Python programming and best practices, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides comprehensive coverage of essential coding concepts and techniques.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Who uses LeetCode?
What percentage of software engineers have no degree?
What are the pros and cons of multithreading?
Which software is used for MongoDB?
What is an online assessment test for a job?
What is the fastest way to learn software engineering?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.