将已安装的 Python 包作为脚本执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4050120/
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
Execute an installed Python package as a script?
提问by Phillip B Oldham
Is there a way to enable a package to be executed as a script? For example:
有没有办法让包作为脚本执行?例如:
[~]# easy_install /path/to/foo.egg
...
[~]# python -m foo --name World
Hello World
I've tried creating a __main__.pyfile inside my package but it's not being executed (I'm using Python 2.6). The following error is raised:
我试过__main__.py在我的包中创建一个文件,但它没有被执行(我使用的是 Python 2.6)。出现以下错误:
foo is a package and cannot be directly executed
The structure of my package is as follows:
我的包的结构如下:
foo/
setup.py
foo/
__init__.py
__main__.py
Running python -m foo.__main__ --name Worldworks as expected, but I would prefer the former way of execution. Is this possible?
运行python -m foo.__main__ --name World按预期工作,但我更喜欢前一种执行方式。这可能吗?
回答by mossplix
as long as the package is on the python path, add at the end of the script.
只要包在python路径上,就在脚本末尾添加。
if __name__ == "__main__":
call_script()
$ python -m module_name
will run the module e.g
将运行模块,例如
python -m random
回答by nfirvine
I think this may be a limitation of Python 2.6. I've tested it, and executing a package (either in .or installed from an egg with easy_install) with the -moption works fine in 2.7, but not in 2.6. For example, on my system (Ubuntu) with a test package called pkg_execin the current directory, and where __main__.pysimply prints sys.argv:
我认为这可能是 Python 2.6 的一个限制。我已经对其进行了测试,并且在 2.7 中.使用该-m选项执行包(在鸡蛋中或从鸡蛋中安装)在 2.7 中运行良好,但在 2.6 中不起作用。例如,在我的系统 (Ubuntu) 上pkg_exec,在当前目录中调用了一个测试包,并在其中__main__.py简单地打印sys.argv:
xx@xx:~/tmp/pkg_exec$ python2.6 -m pkg_exec
/usr/bin/python2.6: pkg_exec is a package and cannot be directly executed
xx@xx:~/tmp/pkg_exec$ python2.7 -m pkg_exec
['/home/xx/tmp/pkg_exec/pkg_exec/__main__.py']
Also, according to the 2.7 docs:
另外,根据2.7 文档:
Changed in version 2.7: Supply the package name to run a
__main__submodule.
在 2.7 版更改: 提供包名称以运行
__main__子模块。
回答by nikcub
This is a regression in Python 2.6. See issue2571:
这是 Python 2.6 中的回归。见issue2571:
The ability to execute packages was never intended, since doing so breaks imports in a variety of subtle ways. It was actually a bug in 2.5 that it was permitted at all, so 2.6 not only disabled it again, but also added a test to make sure it stays disabled (2.4 correctly rejected it with an ImportError, just as 2.6 does).
从来没有打算执行包的能力,因为这样做会以各种微妙的方式破坏导入。它实际上是 2.5 中的一个错误,它完全被允许,因此 2.6 不仅再次禁用它,而且还添加了一个测试以确保它保持禁用状态(2.4 正确地用 ImportError 拒绝了它,就像 2.6 所做的那样)。
You have a few options, you can either always run it specifying main:
您有几个选项,您可以始终指定 main 运行它:
$ python -m module.__main__
Or you can write a shell script wrapper that detects the python version and then executes it in the different style.
或者您可以编写一个 shell 脚本包装器来检测 python 版本,然后以不同的样式执行它。
Or you can execute code on the command line that will import and then run the module, and then perhaps place that in a shell script:
或者您可以在命令行上执行将导入然后运行模块的代码,然后将其放置在 shell 脚本中:
$ python -c "import module; module.main()"
In my own command-line projects I have both the shell script that catches errors (python not being installed, etc.) but the shell script will also execute the import code and detect if the necessary modules have been installed and prompt an error (with a helpful link or install text).
在我自己的命令行项目中,我有两个捕获错误的 shell 脚本(未安装 python 等),但 shell 脚本还将执行导入代码并检测是否已安装必要的模块并提示错误(使用有用的链接或安装文本)。

