English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python main() Function

In this tutorial, we will learn how to dynamically run Python programs in different contexts using the __name__ attribute of the Python program.

What is the main() function in Python?

Some programming languages have a special function called main(), which is the execution point of the program file. However, the Python interpreter runs each line of the file sequentially from the top and does not have an explicit main() function.

Python provides other conventions to define the execution point. One of them is to use the main() function of the Python file and the __name__ attribute.

What is __name__ in Python?

The __name__ variable is a special built-in Python variable that displays the name of the current module.

Depending on the location from which the Python file is executed, it can have different values. Let's look at an example.

running Python files as scripts

Assuming we have a file namedhelloworld.pyThe Python file content is as follows:

print(__name__)

If we runhelloworld.pyIf it is run as a Python script, we can run Python programs using the following command:

python helloworld.py
def foo():
    str="__main__"
    print(str);
if __name__ == "__main__":
    foo()

When we run the program as a script, the value of the variable __name__ is set to__main__. Therefore, the output of the following program will be:

__main__

Running Python file as a module

We can also run a Python file as a module. To do this, we must import this file into another Python program. Let's look at an example.

Suppose we have a file namedmain.pyThe Python file. It has the following content:

import helloworld

When running this file, the following output will be displayed:

helloworld

Here, we can see that importing a module also runs all the code in the module file.

But, we can see, instead of displaying __main__, the program will display the name of the module, that is helloworld.

This is because, when the Python file is run as a module, the name of the module itself is assigned to the __name__ variable.

Using if condition with __name__

Now that we have learned how the __name__ variable is assigned values, we can use the if conditional clause to run the same Python file in different contexts in different ways.

Let's look at an example.

Suppose we willhelloworld.pyThe content of the file is changed to the following:

def main():
    print("Hello World")
if __name__ == "__main__":
    main()

Now, when we run it as a script through the command line, the output will be:

Hello World

However, when we import itmain.pyWhen the file runs as a module, no output is displayed because the main() function is not called.

Here, we create a custom main() function in the helloworld.py file. It only executes when the program is run as a standalone script and not as an imported module.

This is the standard method for explicitly defining the main() function in Python. It is one of the most popular use cases for the Python file __name__ variable.