如何打包命令行Python脚本
我创建了一个旨在从命令行使用的 python 脚本。 我该如何包装它? 这是我的第一个 python 包,我已经阅读了一些有关 setuptools 的内容,但我仍然不确定执行此操作的最佳方法。
解决方案
我最终使用了 setup.py ,其关键配置如下:
setup(
....
entry_points="""
[console_scripts]
mycommand = mypackage.mymodule:main
""",
....
)
这是一个很好的 示例在上下文中。
I've created a python script that's intended to be used from the command line. How do I go about packaging it? This is my first python package and I've read a bit about setuptools, but I'm still not sure the best way to do this.
Solution
I ended up using setup.py with the key configurations noted below:
setup(
....
entry_points="""
[console_scripts]
mycommand = mypackage.mymodule:main
""",
....
)
Here's a good example in context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以使用
scripts
参数直接依赖distutils
setup 函数,而不是使用 setuptools 非标准方式进行操作,如下所述:http://docs.python.org/distutils/setupscript.html#installing-scripts它允许你可以a)与所有python版本保持兼容,b)不必依赖setuptools作为外部依赖项。
Rather than using setuptools non standard way of proceeding, it is possible to directly rely on
distutils
setup's function, using thescripts
argument, as stated here: http://docs.python.org/distutils/setupscript.html#installing-scriptsIt allows you to stay compatible a) with all python versions and b) not having to rely on a setuptools as an external dependency.
@Zach,考虑到您在评论中对@soulmerge的答案的澄清,看起来您需要的是按照有关 distutils -- 这里特别介绍了如何在 pypi 上注册,这里介绍了如何注册注册后上传到 pypi - 并且可能(如果您需要 distutils 自己提供的一些额外功能)通过说明添加 setuptools,其中
easy_install
是其中的一部分 此处。@Zach, given your clarification in your comment to @soulmerge's answer, it looks like what you need is to write a setup.py as per the instructions regarding the distutils -- here in particular is how you register on pypi, and here on how to upload to pypi once you are registrered -- and possibly (if you need some extra functionality wrt what the distutils supply on their own) add setuptools, of which
easy_install
is part, via the instructions here.上个月,我写了一篇文章来准确回答你的问题。 您可以在这里找到它: http://gehrcke.de/ 2014/02/distributing-a-python-command-line-application/
在那里,我仅使用当前推荐的方法(twine、纯 setuptools 而不是 distutils、console_scripts 中的
console_scripts
键)entry_points
字典,...),适用于 Python 2 和 3。Last month, I have written an article answering exactly your question. You can find it here: http://gehrcke.de/2014/02/distributing-a-python-command-line-application/
There, I am using only currently recommended methods (twine, pure setuptools instead of distutils, the
console_scripts
key in theentry_points
dictionary, ...), which work for Python 2 and 3.你说的包装是什么意思? 如果它是在已安装 python 的机器上运行的单个脚本,则只需放置 shebang 进入文件的第一行,就是这样。
如果你希望它在 Windows 下或没有 python 的机器上执行,你将需要一些外部的东西,比如 pyinstaller< /a>.
如果您的问题是关于在哪里放置配置/数据文件,则需要依赖于平台来工作(例如写入 注册表 或主文件夹 >) 据我所知。
What do you mean by packaging? If it is a single script to be run on a box that already has python installed, you just need to put a shebang into the first line of the file and that's it.
If you want it to be executed under Windows or on a box without python, though, you will need something external, like pyinstaller.
If your question is about where to put configuration/data files, you'll need to work platform-dependently (like writing into the registry or the home folder), as far as I know.
对于那些 Python 打包的初学者,我建议阅读这个 Python 打包教程 。
关于教程的注意事项:
For those who are beginners in Python Packaging, I suggest going through this Python Packaging Tutorial.
Note about the tutorial: