In this guide, we will be looking at What does if name == “main”: do? When writing a program before executing any code, the Python interpreter reads the source file; and defines few special variables/global variables. Now, if the source file from the main program is being run by the interpreter; then, the special __name__ variable is set to __main__. Because generally when a file is imported from any module; the __name__ variable is changed to the name of that module and also this name is a global variable.

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

Modules in Python are any file that contains certain definitions and/or statements. The name of the file then contains a special suffix extension .py that serves as an extension to the name. Say, for example,

Now, to discuss the topic of note here let’s begin with a program to give us a first-hand experience of; what we are going to take on.

What does if __name__ == "__main__": do?

Executing the __main__ directly in Python

# a program example to execute __main__ directly

print ("Default Execution")
if __name__ == "__main__":          # providing our condition
    print ("Executed by direct invoking")
    print ("Executed by importing")

Here, it is worth noting that all the code written in the first block will always execute. Also, do note that the well-defined functions and classes that we have well-defined do not always execute. Because we have provided the condition that if the special __name__ variable has the value as __main__; then, and then only shall the program execute. Otherwise, it shall not execute simply rather than importing only. So, we can still check if the script executes by running the program with a valid module name; in the special __name__ variable.

Thus, we can test whether our script will execute directly; or will it execute due to importing it by testing the value in the special __name__ variable.

Now, let’s take another program example wherein we execute a function directly.

Executing a function directly in Python

Here we are about to see a program example wherein we will be executing a function directly. So, what are we waiting for let’s get going?

# a program example to execute a function directly
def func():
    print ("This is Shubham's creation")
# Let us call the function here

Note that the above program executes and gives us the required output. But, it is worth noting that if we want to use that function by importing; then we have to use the code in the way that I am mentioning in the program example below.

# a program example to execute a function directly using __main__

if __name__ == "__main__":
import myscript

Doing as above ensures that:

  1. All modules in Python have the special variable __name__ defined as the __main__ of the program. This implies that the program is being run as a standalone application; and the user can continue doing appropriate corresponding actions as required or necessary.
  2. Also, as discussed above in the earlier section if we import this script as any module in anotherprogram; then, the special __name__ variable changes to the name of that program/module.
  3. Taking due care, note that all Python files either are standalone programs or are versions of reusable modules.

Let’s take one more example :

from file_two import three
print("File one __name__ is set to: {}" .format(__name__))

def one():
   print("Function one gets executed")

def two():
   print("Function two is executed")

if __name__ == "__main__":
   print("File one is executing when this is run directly")
   print("File one is executing when being imported")

Here’s a quick overview of MongoDB for Python, a well-curated article just for you !!

So, what is all the fuss about? What does if name == “main“: do?

The above examples show the importance of the __name__==”__main__” statement. It signifies that you can check and test if the program is executing directly or if it is not executing directly. We simply write the condition checking if __name__==”__main__” and proceed to get the desired output.

Think of this, what if you intend to create a utility module in order to call it and use it in your further projects; in that case, this special variable assigning comes in quite handy really.

This might all seem very confusing but never mind just remember this if __name__==”__main__” returns true then the file is executing directly. And, if the __name__==”__main__” value returns false then the module that is running is coming from another module.

WRAPPING UP !! What does if name == “main“: do?

Hoping that this reference guide clears all the doubts and queries that you have. Also, this post is supposedly a guiding light for you to deal with __name__==”__main__” question, as to what does it do? I thus, write this post is thus in all earnest to act as the all-you-need reference to deal with this topic that we have at hand. With this thought, I end this article. Until next time !! See-ya 🙂

Categorized in: