If you are learning Python, you have probably seen this line many times:
if __name__ == "__main__":
But beginners often ask:
- What is
__name__? - Why do we compare it with
"__main__"? - When should we use it?
- What happens if we don’t use it?
In this article, you will fully understand __name__ == "__main__" in Python, with simple explanations and real examples.
What is __name__ in Python?
__name__ is a special built-in variable in Python.
Python automatically assigns a value to __name__ for every Python file.
Two possible values of __name__:
| Situation | Value of __name__ |
|---|---|
| File is run directly | "__main__" |
| File is imported | "filename" |
Why Does Python Use __name__ == "__main__"?
This condition helps Python decide:
Should this code run now, or should it only be available for import?
It allows us to separate executable code from reusable code.
Example 1: Simple Python File Without __main__
def callMe(name):
print(f"Hello {name}")
callMe("Harry")
Problem ❌
If you import this file into another file, the callMe() function runs automatically, which is not desirable.
Example 2: Correct Way Using __name__ == "__main__"
def callMe(name):
print(f"Hello {name}")
if __name__ == "__main__":
callMe("Harry")
What happens here?
callMe() is defined
The function is executed only when the file is run directly
When imported, nothing runs automatically
Example 3: Running the File Directly
python callMe.py
Output:
Hello Harry
Because Python sets:
__name__ = "__main__"
Example 4: Importing the File into Another Python File
import callMe
callMe.callMe("Harry fernandes")
Output:
Hello Harry fernandes
✔️ The original callMe("Harry") does not run
✔️ Only the imported function is used
Why __name__ == "__main__" is Important in Real Projects
This concept is used everywhere in professional Python development:
- Large Python applications
- Libraries and modules
- Django & Flask projects
- Automation scripts
- Data Science programs
- Interview questions
Example 5: Multiple Functions with __main__
def add(a, b):
return a + b
def subtract(a, b):
return a - b
if __name__ == "__main__":
print(add(10, 5))
print(subtract(10, 5))
Output:
15
5
What Happens If We Remove __name__ == "__main__"?
def add(a, b):
return a + b
print(add(10, 5))
Issue ❌
When imported, this code executes automatically.
This is bad practice and can cause:
- Unexpected output
- Bugs in large applications
- Confusion during imports
Real-World Analogy (Easy to Remember)
Think of a Python file as a tool:
- Functions → tools
__main__block → test/demo area
When you run the file, you test the tool.
When you import the file, you only use the tool.
When Should You Use __name__ == "__main__"?
You should use it when:
✔️ Writing reusable Python modules
✔️ Creating libraries
✔️ Writing scripts with test code
✔️ Working on large projects
✔️ Preparing code for production
Common Interview Question Answer
Q: What is the purpose of __name__ == "__main__" in Python?
Answer:
It ensures that code runs only when the file is executed directly, not when it is imported as a module.
Final Summary
__name__ is a built-in variable
"__main__" means the file is running directly
This condition prevents unwanted execution
It improves code structure and reusability
Used in professional Python development