What does if ` __name__ ` == ` “__main__” `: do?

In this article, we will learn about what does if __name__ == "__main__" do? Before diving into deep, its simple meaning is:

It is just a simple if block where, if var __name__ is equal to string "__main__" then the indented code will run, if not then the code won’t run.

Now lets discuss this in more detail about what does __name__ and "__main__" means? See the code sinppet below.

Here we have two .py files:

  • script.py: In this file we have our main code, lets say an age verifier function. Where user inputs his/her age and this returns his/her age.
  • code.py: In this file, script.py has been imported and from script.py, verifyAge() function has been imported.

CODE:

Advertisements

script.py

age = input('Enter Your Age: ')

def verifyAge(age):
    if __name__ == "__main__":
        print('Age of the user: ',age)
    else:
        print('Imported in some other script as a module, Your age is: ',age)


verifyAge(age)

OUTPUT :

Enter Your Age: 25
Age of the user:  25

code.py

CODE :

from script import verifyAge

OUTPUT :

Enter Your Age: 26
Imported in some other script as a module, Your age is:  26

So in the above code snippets, you can see there are different outputs in the function verifyAge().

  • Here comes the "__main__" which is just like main() function of the C programming language or fun main() in Kotlin, which states that this is the main function from where the rest of the code runs. Just like when you create an another function in C programming and run it the void main() or int main() function. This"__main__" variable states the same in Python Programming language. As you can see everything in the indentation level 0 gets executed first and then rest of the function and code, same as the code in int main() or void main() gets executed first in C Programming language and then rest of the code.
  • Now, What does the __name__ implies? __name__ is a special variable in python programming language which means the name of the module without the .py extension.

Now back to our question What does if name ==main: do?. In Python Programming language, when a module is running as the main program(or in the same module as a entry point code), then the __name__ variable is set to "__main__". In the above code snipppet also you can see when the code is being run in the script.py file as the entry point code, then the verifyAge function only returns the age of the user. But when the verifyAge() function is imported in code.py the __name__ variable is set here to the name of the program and now the code goes in the else block. That’s why you can spot the differecne in the output.

Summary

In this article we learned about a basic syntax of Python Programming which creates a great value in Python. In short, if __name__ == "__main__" : is a if-else conditional block which checks whether the code is running as the main function(entry point) or it is being imported in some other module. Python sets the __name__ variable to "__main__" if the code is being run as the entry point or in the same module/script. If the code is imported in some other script, then the Python Intrepeter sets the __name__ variable to the name of the script/module.

It is useful when we want a certain block of code not to be executed in some other module. For example if we want to run a code only in main file not in some other .py file where this file is being imported as a module.

Make sure to practice with the code samples from this article, to have a better understanding of this problem. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top