如何使用setuptools生成一个console_scripts入口点,该入口点调用`python -m mypackage`? [英] How can I use setuptools to generate a console_scripts entry point which calls `python -m mypackage`?
问题描述
我正努力成为一名优秀的Pythonista使用者,并按照 PEP 338 进行操作我计划部署的程序包.
I am trying to be a good Pythonista and following PEP 338 for my package I plan on deploying.
我也在尝试使用setuptools entry_points{'console_scripts': ... }
选项在python setuptools install
上生成我的可执行脚本.
I am also trying to generate my executable scripts upon python setuptools install
using setuptools entry_points{'console_scripts': ... }
options.
如何使用entry_points生成调用python -m mypackage
的二进制文件(并传递* args,** kwargs)?
How can I use entry_points to generate a binary that calls python -m mypackage
(and passes *args, **kwargs) ?
以下是我没有成功的尝试:
Here are a few attempts I have made with no success:
setuptools(
...
(1)
entry_points=
{'console_scripts': ['mypkg=mypkg.__main__'],},
(2)
entry_points=
{'console_scripts': ['mypkg=mypkg.main'],},
(3)
entry_points=
{'console_scripts': ['mypkg=python -m mypkg'],},
我一直在使用的主要资源:
Primary resources I have been using:
- http://pythonhosted.org/setuptools/setuptools.html#automatic-脚本创建
- https://www.python.org/dev/peps/pep-0338/
- http://www.scotttorborg.com/python-packaging/command-line-scripts.html
- http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/
- http://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation
- https://www.python.org/dev/peps/pep-0338/
- http://www.scotttorborg.com/python-packaging/command-line-scripts.html
- http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/
推荐答案
如何使用entry_points生成调用python -m mypackage的二进制文件(并传递* args,** kwargs)?
How can I use entry_points to generate a binary that calls python -m mypackage (and passes *args, **kwargs) ?
我认为这是解决问题的错误方法.您不希望脚本调用python -m mypackage
,但是希望脚本具有与python -m mypackage
I think this is the wrong way to look at the problem. You don't want your script to call python -m mypackage
, but you want the script to have the same entry point as python -m mypackage
考虑以下简单示例:
script_proj/
├── script_proj
│ ├── __init__.py
│ └── __main__.py
└── setup.py
和简约的setup.py:
and the minimalistic setup.py:
from setuptools import setup
setup(
name="script_proj",
packages=["script_proj"],
entry_points = {
"console_scripts": [
"myscript = script_proj.__main__:main",
]
}
)
__main__.py
是虚拟模块,包含main
方法.
__main__.py
is a dummy module and contains the main
method.
def main():
print("Hello world!")
if __name__ == "__main__":
main()
安装后,您将拥有可执行文件myscript
,该可执行文件将在__main__.py
中调用main
方法.
在此包装设计中,python -m script_proj
也调用相同的main
方法.
After installing, you have the executable myscript
, which calls the main
method in __main__.py
.
In this package design python -m script_proj
also calls the same main
method.
这篇关于如何使用setuptools生成一个console_scripts入口点,该入口点调用`python -m mypackage`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!