__main__
— Top-level code environment¶
在 Python 中,特殊名称 __main__
用于两个重要的结构:
程序顶层环境的名称,可以用
__name__ == '__main__'
表达式来检查;以及Python 软件包中的
__main__.py
文件。
这两种机制都与 Python 模块有关;用户如何与它们交互以及它们之间如何交互。下面对此进行了详细的解释。如果你是 Python 模块的新手,请参阅教程部分 模块 的介绍。
__name__ == '__main__'
¶
当一个 Python 模块或包被导入时,__name__
被设置为模块的名称。通常,这是 Python 文件本身的名称,没有 .py
扩展名
>>> import configparser
>>> configparser.__name__
'configparser'
如果该文件是一个包的一部分,__name__
也将包括父包的路径
>>> from concurrent.futures import process
>>> process.__name__
'concurrent.futures.process'
然而,如果该模块在顶层代码环境中执行,它的 __name__
被设置为字符串 '__main__'
。
什么是 “顶层代码环境”?¶
__main__
是运行顶层代码的环境名称。”顶层代码” 是第一个开始运行的用户指定的 Python 模块。它是 “顶层代码” 因为它导入了程序需要的所有其它模块。有时,”顶层代码” 被称为应用程序的 入口点。
顶层的代码环境可以是:
交互提示的范围
>>> __name__ '__main__'
作为文件参数传递给 Python 解释器的 Python 模块:
$ python3 helloworld.py Hello, world!
通过
-m
参数传递给 Python 解释器的 Python 模块或包:$ python3 -m tarfile usage: tarfile.py [-h] [-v] (...)
Python 解释器从标准输入读取的 Python 代码:
$ echo "import this" | python3 The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. ...
用
-c
参数传递给 Python 解释器的 Python 代码:$ python3 -c "import this" The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. ...
在每一种情况下,顶层模块的 __name__
被设置为 '__main__'
。
因此,一个模块可以通过检查它自己的 __name__
来得知是否在顶层环境中运行,这就允许在模块没有从导入语句中初始化时,有条件地执行代码的常见习语
if __name__ == '__main__':
# Execute when the module is not initialized from an import statement.
...
参见
关于在所有情况下如何设置 __name__
的问题,请看教程部分 模块。
习语用法¶
有些模块包含了仅供脚本使用的代码,比如解析命令行参数或从标准输入获取数据。如果像这样的模块被从不同的模块中导入,例如为了单元测试,脚本代码也会无意中执行。
这就是使用 if __name__ == '__main__'
代码块的用武之地。除非模块在顶层环境中被执行,否则该块中的代码不会运行。
在 if __name___ == '__main__'
下面的块中放入尽可能少的语句可以提高代码的清晰度和正确性。大多数情况下,一个名为 main
的函数封装了程序的主要行为
# echo.py
import shlex
import sys
def echo(phrase: str) -> None:
"""A dummy wrapper around print."""
# for demonstration purposes, you can imagine that there is some
# valuable and reusable logic inside this function
print(phrase)
def main() -> int:
"""Echo the input arguments to standard output"""
phrase = shlex.join(sys.argv)
echo(phrase)
return 0
if __name__ == '__main__':
sys.exit(main()) # next section explains the use of sys.exit
注意,如果模块没有在 main
函数内封装代码,而是直接放在 if __name__ == '__main__'
块内,phrase
变量将对整个模块是全局的。这很容易出错,因为模块中的其他函数可能会无意中使用全局变量而不是局部名称。一个 main
函数解决了这个问题。
使用 main
函数有一个额外的好处,就是 echo
函数本身是孤立的,可以在其他地方导入。当 echo.py
被导入时,echo
和 main
函数将被定义,但它们都不会被调用,因为 __name__ != '__main__'
。
包装方面的考虑¶
main
functions are often used to create command-line tools by specifying
them as entry points for console scripts. When this is done,
pip inserts the function call into a template script,
where the return value of main
is passed into sys.exit()
.
For example:
sys.exit(main())
Since the call to main
is wrapped in sys.exit()
, the expectation is
that your function will return some value acceptable as an input to
sys.exit()
; typically, an integer or None
(which is implicitly
returned if your function does not have a return statement).
By proactively following this convention ourselves, our module will have the
same behavior when run directly (i.e. python3 echo.py
) as it will have if
we later package it as a console script entry-point in a pip-installable
package.
In particular, be careful about returning strings from your main
function.
sys.exit()
will interpret a string argument as a failure message, so
your program will have an exit code of 1
, indicating failure, and the
string will be written to sys.stderr
. The echo.py
example from
earlier exemplifies using the sys.exit(main())
convention.
参见
Python Packaging User Guide contains a collection of tutorials and references on how to distribute and install Python packages with modern tools.
__main__.py
in Python Packages¶
If you are not familiar with Python packages, see section 包
of the tutorial. Most commonly, the __main__.py
file is used to provide
a command-line interface for a package. Consider the following hypothetical
package, “bandclass”:
bandclass
├── __init__.py
├── __main__.py
└── student.py
__main__.py
will be executed when the package itself is invoked
directly from the command line using the -m
flag. For example:
$ python3 -m bandclass
This command will cause __main__.py
to run. How you utilize this mechanism
will depend on the nature of the package you are writing, but in this
hypothetical case, it might make sense to allow the teacher to search for
students:
# bandclass/__main__.py
import sys
from .student import search_students
student_name = sys.argv[2] if len(sys.argv) >= 2 else ''
print(f'Found student: {search_students(student_name)}')
Note that from .student import search_students
is an example of a relative
import. This import style can be used when referencing modules within a
package. For more details, see 子包参考 in the
模块 section of the tutorial.
习语用法¶
The contents of __main__.py
typically isn’t fenced with
if __name__ == '__main__'
blocks. Instead, those files are kept short,
functions to execute from other modules. Those other modules can then be
easily unit-tested and are properly reusable.
If used, an if __name__ == '__main__'
block will still work as expected
for a __main__.py
file within a package, because its __name__
attribute will include the package’s path if imported:
>>> import asyncio.__main__
>>> asyncio.__main__.__name__
'asyncio.__main__'
This won’t work for __main__.py
files in the root directory of a .zip file
though. Hence, for consistency, minimal __main__.py
like the venv
one mentioned below are preferred.
参见
See venv
for an example of a package with a minimal __main__.py
in the standard library. It doesn’t contain a if __name__ == '__main__'
block. You can invoke it with python3 -m venv [directory]
.
See runpy
for more details on the -m
flag to the
interpreter executable.
See zipapp
for how to run applications packaged as .zip files. In
this case Python looks for a __main__.py
file in the root directory of
the archive.
import __main__
¶
Regardless of which module a Python program was started with, other modules
running within that same program can import the top-level environment’s scope
(namespace) by importing the __main__
module. This doesn’t import
a __main__.py
file but rather whichever module that received the special
name '__main__'
.
Here is an example module that consumes the __main__
namespace:
# namely.py
import __main__
def did_user_define_their_name():
return 'my_name' in dir(__main__)
def print_user_name():
if not did_user_define_their_name():
raise ValueError('Define the variable `my_name`!')
if '__file__' in dir(__main__):
print(__main__.my_name, "found in file", __main__.__file__)
else:
print(__main__.my_name)
Example usage of this module could be as follows:
# start.py
import sys
from namely import print_user_name
# my_name = "Dinsdale"
def main():
try:
print_user_name()
except ValueError as ve:
return str(ve)
if __name__ == "__main__":
sys.exit(main())
Now, if we started our program, the result would look like this:
$ python3 start.py
Define the variable `my_name`!
The exit code of the program would be 1, indicating an error. Uncommenting the
line with my_name = "Dinsdale"
fixes the program and now it exits with
status code 0, indicating success:
$ python3 start.py
Dinsdale found in file /path/to/start.py
Note that importing __main__
doesn’t cause any issues with unintentionally
running top-level code meant for script use which is put in the
if __name__ == "__main__"
block of the start
module. Why does this work?
Python inserts an empty __main__
module in sys.modules
at
interpreter startup, and populates it by running top-level code. In our example
this is the start
module which runs line by line and imports namely
.
In turn, namely
imports __main__
(which is really start
). That’s an
import cycle! Fortunately, since the partially populated __main__
module is present in sys.modules
, Python passes that to namely
.
See Special considerations for __main__ in the
import system’s reference for details on how this works.
The Python REPL is another example of a “top-level environment”, so anything
defined in the REPL becomes part of the __main__
scope:
>>> import namely
>>> namely.did_user_define_their_name()
False
>>> namely.print_user_name()
Traceback (most recent call last):
...
ValueError: Define the variable `my_name`!
>>> my_name = 'Jabberwocky'
>>> namely.did_user_define_their_name()
True
>>> namely.print_user_name()
Jabberwocky
Note that in this case the __main__
scope doesn’t contain a __file__
attribute as it’s interactive.
The __main__
scope is used in the implementation of pdb
and
rlcompleter
.