if _name_== ‘_main_’ in Python

If you are someone who has been into Python programming for a while now, you might have gone through a lot of programs. But if you have observed the programs, in many of the programs, you might see some instructions like this – “if __name__ == ‘__main__'”. But have you ever wondered what does this instruction mean? Let’s find it out in this article.

See, let’s assume that you are already familiar with the “if” conditional statement, using which, you can write instructions that can be executed on the basis of some conditions. Now, the “__name__ == ‘__main__'” is actually a comparison, in which we are checking if the __name__ variable has the value ‘__main__’. Well, to understand this more properly, you need to understand what the __name__ variable holds in Python.

See, in short, the value in __name__ variable depends on how the containing script is executed. If you are directly executing the program, the __name__ variable holds the value ‘__main__’. If you are importing your script to another program, the __name__ variable holds the name of the module.

So, by writing the instruction “if __name__ == ‘__main__’:”, we are saying that we need to execute the following code only if the script is itself being executed.

Here is an example to understand the instruction “if __name__ == ‘__main__’:”.

Let’s assume that we are not familiar with this instruction at all. In that case, we would write a Python program, which is something like this –

def printName():
print("GyaniPandit")

printName()

The above program is a super simple program, where we wrote a function to print “GyaniPandit”, and then we called that function.

I hope you are already familiar with the fact, that we can import our Python programs into other Python programs(if not, then you are familiar now). Let’s say that the above program name is program1.py. Now, let’s say we have another program, program2.py, and we are importing the program1 into the program2. Here is what the program looks like –

import program1

print("We are inside the program2 program file")

If you execute the above program, you will see the output as something like this –

Output –

GyaniPandit
We are inside the program2 program file

The “GyaniPandit” as you can see, comes from the program1 file. This is because we are calling the printName function in the program1, and we are also importing it, so it is being executed. But if we make some changes to the program1 code, and make it like this –

def printName():
print("GyaniPandit")

if __name__ == '__main__':
printName()

We just made one change, by adding the instruction “if __name__ == ‘__main__’:”. After making the changes to the program1 file, let’s execute the program2 file again. Here is the output of the program2 file now –

Output Here –

We are inside the program2 program file

As you can see, this time, we are not seeing “GyaniPandit” in the output, like the last time. This is because this time, we have specified that you have to call the function only if the program is being executed, and not imported into another program. Since we were importing the program, the function didn’t get called.

So, I hope that through the simple example, you can understand the instruction “if __name__ == ‘__main__’:” in our Python programs. Now, you can also use this instruction in your Python programs, as and when required.

You can also refer to this video, for a detailed explanation of why we use the instruction “if __name__ == ‘__main__’:” in our Python programs.