What is __main__.py?
通常,通过在命令行中命名.py文件来运行python程序:
1 | $ python my_program.py |
您还可以创建一个包含代码的目录或压缩文件,并包括一个
1 2 3 4 | $ python my_program_dir $ python my_program.zip # Or, if the program is accessible as a module $ python -m my_program |
您必须自己决定是否可以从这样的执行中获益。
注意,
如果您在错误消息中看到名称
创建python模块时,通常让模块在作为程序入口点运行时执行一些功能(通常包含在
1 2 3 | if __name__ == '__main__': # execute only if run as the entry point into the program main() |
对于使用
1 2 3 4 5 6 7 8 9 10 11 | $ mkdir demo $ cat > demo/__init__.py << EOF print('demo/__init__.py executed') def main(): print('main executed') EOF $ cat > demo/__main__.py << EOF print('demo/__main__.py executed') from __init__ import main main() EOF |
(在posix/bash shell中,通过在每个cat命令末尾输入ctrl+d(文件结束字符),可以在不使用
现在:
1 2 3 4 | $ python demo demo/__main__.py executed demo/__init__.py executed main executed |
你可以从文档中得到这个。文件上说:
__main__ — Top-level script environment
'__main__' is the name of the scope in which top-level code executes.
A module’s__name__ is set equal to'__main__' when read from standard
input, a script, or from an interactive prompt.A module can discover whether or not it is running in the main scope
by checking its own__name__ , which allows a common idiom for
conditionally executing code in a module when it is run as a script or
withpython -m but not when it is imported:
1
2
3 if __name__ == '__main__':
# execute only if run as a script
main()For a package, the same effect can be achieved by including a
__main__.py module, the contents of which will be executed when the module is run with-m .
拉链
您也可以将它打包成一个单独的文件,然后像这样从命令行运行它-但是请注意,压缩包不能作为入口点执行子包或子模块:
1 2 3 4 5 | $ python -m zipfile -c demo.zip demo/* $ python demo.zip demo/__main__.py executed demo/__init__.py executed main() executed |
1 2 | test.zip __main__.py |
其中
1 2 | import sys print"hello %s" % sys.argv[1] |
然后,如果我们运行
因此,当对zip文件调用python时,
在
1 | $ python -m yourpackage |
如果脚本是目录或zip文件而不是单个python文件,则当"script"作为参数传递给python解释器时,将执行