Home What is Python __name__ == "__main__"?
Post
Cancel

What is Python __name__ == "__main__"?

You might have seen the Python line if __name__ == "__main__": in several code examples and wonder what does it do? Many other programming languages such as Java or C/C++ require a main function as the starting point for an application. However, in Python you can type some lines of code in a file, and the code is executed just fine without the need for a main. So why is there a need for adding if __name__ == "__main__" to your Python modules? We first need to look at how Python deals with imports to figure that out.

Suppose we have two files. One is the starting point of our application and will be passed to the Python interpreter called start.py:

1
2
3
4
import my_module

print("This is start.py!")
my_module.my_function(42)

And a second file contains the module imported by start.py called my_module.py:

1
2
3
4
5
def my_function(a):
    print(f"my_function got {a}")

print("This is my_module!")
my_function(23)

When passing start.py to the Python interpreter, it will first check if the file and all imports are syntactically correct. When the Python code has valid syntax, the interpreter will execute each line starting from the top. When the interpreter encounters an import statement, the code inside the imported module or package is executed as well, which leads to the following output:

1
2
3
4
This is my_module!
my_function got 23
This is start.py!
my_function got 42

We can see that the print statement print("This is my_module!") and the call of my_function from my_module.py is executed before the print and the function call of my_function in start.py. Because the Python interpreter executes each line of each file, it opens. This behavior could lead to unwanted side effects where code from a different file is executed silently and could declare global variables never intended to be set.

You might ask yourself, why would anyone write this code? It is not advised to add code not enclosed by a function or class to a module imported by another module; however, sometimes you might want the module to be executable by itself, and then you have to have such code.

The unintended execution of code through imports can be easily fixed by adding the check if __name__ == "__main__": and guarding the code that is not inside a function or a class from execution.

my_package.py

1
2
3
4
5
6
def my_function(a):
    print(f"my_function got {a}")

if __name__ == "__main__":
    print("This is my_package!")
    my_function(23)

start.py

1
2
3
4
5
import my_package

if __name__ == "__main__":
    print("This is main.py")
    my_package.my_function(42)

When we now pass start.py to the Python interpreter it only executes the code indented under if __name__ == "__main__" in start.py. Because the Python interpreter sets a special variable inside each file, it opens, which is __name__. The __name__ variable is set to the file name without the .py. However, for the file passed directly to the Python interpreter, __name__ is set to __main__ and thereby marks the file as the main entry point for our application.

In our example, the variable __name__ in my_module.py is set to my_module, which guards the code in my_module.py from execution when it is imported. And we are still able to execute my_module.py on its own when we pass it to the Python interpreter.

Conclusion

The special variable __name__ is set to the file name of the file the Python interpreter is currently executing except for the file that is the start point for an application, then __name__ is set to __main__. This feature can be used to guard code from execution when importing modules.

This post is licensed under CC BY 4.0 by the author.