What is Python __name__?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

In this article, we shall learn about Python \_\_name__ . \_\_name__ is a special variable in Python that stores the name of the python module/script currently being executed. It was introduced in version 3.0 of Python. If the current script/module is in execution then the Python \_\_name__ variable is assigned the value \_\_main__. Otherwise, the name of the script or module in execution is stored in the \_\_name__ variable.

What is __name__ Variable in Python?

Unlike many other programming languages, Python does not have a main() function. When the interpreter has to begin executing a Python script, the code present at level0 of indentation is executed. The interpreter must first define some special variables, one of which is \_\_name__.

If the file of source code is in execution as the main program, the interpreter assigns the value \_\_main__ to the Python \_\_name__ variable. Otherwise, that is, if the code in execution has been imported from some other script/module, the name of the module is set as the value of the \_\_name__ variable.

Since the Python \_\_name__ variable is inbuilt and yields the name of the module currently in execution, we can use it for determining whether it is the current file that is being executed, or some other imported module, possibly in combination with current file’s code, for example in an if statement, etc.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Using __name__ == __main__

Say we command the Python interpreter to start executing a Python file, say “pythonScript.py”. Assume the following are the code contents of our Python script:

All the code at level 0 indentation is executed. Output python name code output

Since we directly executed the pythonScript.py file, the Python variable \_\_name__ is set to value \_\_main__. The code in the else block runs only if the code in execution is imported. Hence, one may verify whether directly one’s script is executing or some imported script/module.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Printing the Value of __name__ in Python

Let's print the value of the __name__ variable at each stage of execution to help you understand it better.

Example: The python code file pythonScript.py is described below :

Output:

Examples of Using __name__ in Python

The following code block prints the name of the currently executing script, which is __main__:

Output

The following code block prints the data type of the \_\_name__ variable:

Output

The following code block depicts the behavior of Python \_\_name__ in imported modules: myModule.py:

main.py:

Output

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Learn More

You may want to have a look at many cool Python articles here

Conclusion

  • \_\_name__ is a special variable in Python that stores the name of the python module/script currently being executed.
  • Since the \_\_name__ variable is inbuilt and yields the name of the module currently in execution, we can use it for determining whether or not it is the current file that is being executed.