Python __main__.py 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4042905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:59:24  来源:igfitidea点击:

What is __main__.py?

python

提问by Monika Sulik

What is the __main__.pyfile for, what sort of code should I put into it, and when should I have one?

__main__.py文件的用途是什么,我应该将什么样的代码放入其中,以及我应该什么时候拥有一个?

采纳答案by Ned Batchelder

Often, a Python program is run by naming a .py file on the command line:

通常,通过在命令行上命名 .py 文件来运行 Python 程序:

$ python my_program.py

You can also create a directory or zipfile full of code, and include a __main__.py. Then you can simply name the directory or zipfile on the command line, and it executes the __main__.pyautomatically:

您还可以创建一个充满代码的目录或 zip 文件,并包含一个__main__.py. 然后你可以简单地在命令行上命名目录或 zipfile,它会__main__.py自动执行:

$ python my_program_dir
$ python my_program.zip
# Or, if the program is accessible as a module
$ python -m my_program

You'll have to decide for yourself whether your application could benefit from being executed like this.

您必须自己决定您的应用程序是否可以从这样执行中受益。



Note that a __main__moduleusually doesn't come from a __main__.pyfile. It can, but it usually doesn't. When you run a script like python my_program.py, the script will run as the __main__module instead of the my_programmodule. This also happens for modules run as python -m my_module, or in several other ways.

请注意,__main__模块通常不是来自__main__.py文件。它可以,但通常不会。当您运行类似python my_program.py的脚本时,该脚本将作为__main__模块而不是my_program模块运行。这也适用于作为python -m my_module或以其他方式运行的模块。

If you saw the name __main__in an error message, that doesn't necessarily mean you should be looking for a __main__.pyfile.

如果您__main__在错误消息中看到该名称,这并不一定意味着您应该查找__main__.py文件。

回答by Blue Peppers

__main__.pyis used for python programs in zip files. The __main__.pyfile will be executed when the zip file in run. For example, if the zip file was as such:

__main__.py用于 zip 文件中的 python 程序。该__main__.py文件将在运行 zip 文件时执行。例如,如果 zip 文件是这样的:

test.zip
     __main__.py

and the contents of __main__.pywas

和内容__main__.py

import sys
print "hello %s" % sys.argv[1]

Then if we were to run python test.zip worldwe would get hello worldout.

然后,如果我们要跑,python test.zip world我们就会hello world出去。

So the __main__.pyfile run when python is called on a zip file.

因此,__main__.py当在 zip 文件上调用 python 时,该文件会运行。

回答by Wooble

If your script is a directory or ZIP file rather than a single python file, __main__.pywill be executed when the "script" is passed as an argument to the python interpreter.

如果您的脚本是目录或 ZIP 文件而不是单个 python 文件,__main__.py则将在“脚本”作为参数传递给 python 解释器时执行。

回答by anatoly techtonik

You create __main__.pyin yourpackageto make it executable as:

创建__main__.pyyourpackage使其可执行文件:

$ python -m yourpackage

回答by Aaron Hall

What is the __main__.pyfile for?

什么是__main__.py文件?

When creating a Python module, it is common to make the module execute some functionality (usually contained in a mainfunction) when run as the entry point of the program. This is typically done with the following common idiom placed at the bottom of most Python files:

创建 Python 模块时,通常使模块在main作为程序入口点运行时执行某些功能(通常包含在函数中)。这通常通过放置在大多数 Python 文件底部的以下常见习惯用法来完成:

if __name__ == '__main__':
    # execute only if run as the entry point into the program
    main()

You can get the same semantics for a Python package with __main__.py. This is a linux shell prompt, $, if you don't have Bash (or another Posix shell) on Windows just create these files at demo/__<init/main>__.pywith contents in between the EOFs:

您可以获得与 Python 包相同的语义__main__.py。这是一个 linux shell 提示,$如果您在 Windows 上没有 Bash(或另一个 Posix shell),只需demo/__<init/main>__.pyEOFs之间创建这些文件:

$ 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

(In a Posix/Bash shell, you can do the above without the << EOFs and ending EOFs by entering Ctrl+D, the end-of-file character, at the end of each cat command)

(在 Posix/Bash shell 中,您可以通过在每个 cat 命令的末尾输入+ (文件结束字符)来执行上述操作,而无需<< EOFs 和结尾EOFs )CtrlD

And now:

现在:

$ python demo
demo/__main__.py executed
demo/__init__.py executed
main executed

You can derive this from the documention. The documentationsays:

您可以从文档中得出这一点。该文件说:

__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 with python -mbut not when it is imported:

if __name__ == '__main__':
      # execute only if run as a script
      main()

For a package, the same effect can be achieved by including a __main__.pymodule, the contents of which will be executed when the module is run with -m.

__main__— 顶级脚本环境

'__main__'是顶级代码执行的范围的名称。当从标准输入、脚本或交互式提示中读取时,模块的__name__设置等于'__main__'

模块可以通过检查自己的 来发现它是否在主作用域中运行__name__,这允许在模块作为脚本运行时或python -m在导入时有条件地执行模块中的代码的通用习惯用法:

if __name__ == '__main__':
      # execute only if run as a script
      main()

对于一个包,通过包含一个__main__.py模块可以达到同样的效果, 模块的内容将在模块运行时执行-m

Zipped

压缩

You can also package this into a single file and run it from the command line like this - but note that zipped packages can't execute sub-packages or submodules as the entry point:

您也可以将其打包到一个文件中并像这样从命令行运行它 - 但请注意,压缩包不能作为入口点执行子包或子模块:

$ python -m zipfile -c demo.zip demo/*
$ python demo.zip
demo/__main__.py executed
demo/__init__.py executed
main() executed