Python __name__ and __main__

Usually, we often see Python code like this:

if __name__ == "__main__":
    main() 

In Python,__name__and__main__are two special variables related to module and script execution.

__name__and__main___name__ is usually used to control the way code is executed, especially when a module can be run both as a standalone script and be imported by other modules.

1. The __name__ Variable

__name__ is a built-in variable used to represent the name of the current module.

__name__ The value of __name__ depends on how the module is used:

When the module is run as the main program:__name__the value of __name__ is set to"__main__"。

When the module is imported:__name__the value of __name__ is set to the module's filename (excluding the.py.py extension).

Suppose there is a module.py file:

print(f"模块的 __name__ 值: {__name__}")
The output will be:
模块的 __name__ 值: __main__

2. The Meaning of __main__

__main__is a special string used to indicate that the current module is running as the main program.

__main__It is usually used together with the __name__ variable to determine whether a module is imported or run as a standalone script.

3. Common Patterns for Usingif __name__ == "__main__":__name__ == "__main__"

In Python, a common practice is to add the following code block at the end of a module:

Examples

if __name__ == "__main__":
    # The code here will only execute when the module is run as the main program
    main()

This pattern allows the module to not execute certain code when imported, and only execute that code when run as a standalone script.


Example

Suppose we have a module named example.py:

example.py file module:

def greet():
    print("Greetings from the example module!")

if __name__ == "__main__":
    print("The script is being run directly.")
    greet()
else:
    print("The script is being imported as a module.")

Run example.py directly

If you run example.py directly, the output will be:

该脚本正在直接运行。
来自 example 模块的问候!

In this case,__name__the value of __name__ is "__main__", so theif __name__ == "__main__"__name__ == "__main__": block of code will be executed.

Import example.py

If you import example.py in another script, for example:

Example

# another_script.py

import example

example.greet()

The output will be:

该脚本作为模块被导入。
来自 example 模块的问候!

In this case,__name__the value of __name__ is"example" (the module name), so theif __name__ == "__main__":__name__ == "__main__" block of code will not be executed.

Summary

  • __name____name__ is a built-in variable that represents the name of the current module.

  • When the module is run as the main program,__name__the value of __name__ is "__main__"."__main__"。

  • When the module is imported,__name__the value of __name__ is the module's filename.

  • Usingif __name__ == "__main__":__name__ == "__main__" can control the module so that certain code is not executed when imported, but is only executed when run as a standalone script.

Other Extensions