What does if __name__ == “__main__”: do in Python

Hello guys. In this tutorial we will learn that what does if __name__ == “__main__”: do in Python.

if __name__ == “__main__”  in Python prevents the specific lines of code to run when the module is imported. We know that in Python we can import any file with the “.py” extension. It may be any pre-existed module or any user-made program.

So if you’re unknown with the use of if __name__ == “__main__” till now, stay with us because in this post we are going to learn the working of if __name__ == “__main__” in Python.

Also read: Keyword-only argument in Python

if __name__ == “__main__” in Python

The Python interpreter always set a few special variables like “__name__” and execute the whole program(source code). Let’s understand with a decent example.

Suppose we made a program with the name “csmain1.py”. Inside”csmain1.py” a function and other lines of code are present.

#csmain1.py

def mean(*val):
    length=len(val)
    sum=0
    for item in range(0, length):
        sum=sum+val[item]
    return sum/length

print("Codespeedy Technology")
#Calling Function
print(mean(9,8,7,6,5,4))
print("Hello World")

Output:

Codespeedy Technology
6.5
Hello World

Now if we want to use the mean() function in another program and we know that in Python we can import any file with the “.py” extension.
So we will import the csmain1.py in our new program file csmain2.py using import statement in the following way:

#csmain2.py

import csmain1 #csmain1 file is imported here
print(csmain1.mean(12,13,1))

In this way mean() function is called in “csmain2.py” from “csmain1.py”.If we execute the “csmain2.py” we get the output of “csmain1.py”.Let’s see:

Output:

Codespeedy Technology
6.5
Hello World
8.666666666666666

According to our program, the output should be 8.666666666666666 because the only mean() function is called. But the whole program is imported. To overcome this we use if __name__ == “__main__”. The extra line of code written after function mean() is kept inside if __name__ == “__main__” so it can’t be executed while the function is imported in “csmain2.py”.

#csmain1.py

def mean(*val):
    length=len(val)
    sum=0
    for item in range(0, length):
        sum=sum+val[item]
    return sum/length

if __name__ == '__main__':
    print("Codespeedy Technology")
    print(mean(9,8,7,6,5,4))
    print("Hello World")

Output:

Codespeedy Technology
6.5
Hello World

And after importing “csmain1.py” in “csmain2.py”:

#csmain2.py

import csmain1 #csmain1 file is imported here
print(csmain1.mean(12,13,1))

Output:

8.666666666666666

Now we can see that after using if __name__ == “__main__” the unwanted codes can not be used by “csmain2.py”. So after all that we understand that What does if __name__ == “__main__”: do in Python, it prevents certain code to run if any other file import it.

Also read: What is PIP in Python?

Any query related to this topic and any other Python topic can be asked below in the comment section.

Leave a Reply

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