python 如何打包命令行Python脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1117041/
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
How to package a command line Python script
提问by Zach
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.
我创建了一个旨在从命令行使用的 python 脚本。我该如何包装它?这是我的第一个 python 包,我已经阅读了一些关于 setuptools 的内容,但我仍然不确定这样做的最佳方法。
Solution
解决方案
I ended up using setup.pywith the key configurations noted below:
我最终使用setup.py和下面提到的关键配置:
setup(
....
entry_points="""
[console_scripts]
mycommand = mypackage.mymodule:main
""",
....
)
Here's a good examplein context.
这是上下文中的一个很好的例子。
采纳答案by Alex Martelli
@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-- herein particular is how you register on pypi, and hereon 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.
@Zach,鉴于您在对@soulmerge 的回答的评论中进行了澄清,看起来您需要的是按照有关distutils的说明编写 setup.py -这里特别是您如何在 pypi上注册,以及如何在此处注册注册后上传到 pypi - 并且可能(如果您需要一些额外的功能 wrt distutils 自己提供的功能)easy_install
通过此处的说明添加 setuptools,这是其中的一部分。
回答by Alexis Métaireau
Rather than using setuptools non standard way of proceeding, it is possible to directly rely on distutils
setup's function, using the scripts
argument, as stated here: http://docs.python.org/distutils/setupscript.html#installing-scripts
与其使用 setuptools 非标准的处理方式,不如直接依赖distutils
setup 的函数,使用scripts
参数,如下所述:http: //docs.python.org/distutils/setupscript.html#installing-scripts
from distutils import setup
setup(
...,
scripts=['path/to/your/script',],
...
)
It allows you to stay compatible a) with all python versions and b) not having to rely on a setuptools as an external dependency.
它允许您保持兼容 a) 与所有 python 版本和 b) 不必依赖 setuptools 作为外部依赖项。
回答by Dr. Jan-Philip Gehrcke
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/
上个月,我写了一篇文章完全回答了你的问题。你可以在这里找到它: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 the entry_points
dictionary, ...), which work for Python 2 and 3.
在那里,我只使用当前推荐的方法(twine、纯 setuptools 而不是 distutils、字典中的console_scripts
键entry_points
,...),这些方法适用于 Python 2 和 3。
回答by soulmerge
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 shebanginto the first line of the file and that's it.
你说的包装是什么意思?如果它是在一个已经安装了 python 的机器上运行的单个脚本,你只需要在文件的第一行放置一个shebang,就是这样。
If you want it to be executed under Windows or on a box without python, though, you will need something external, like pyinstaller.
但是,如果您希望它在 Windows 下或没有 python 的机器上执行,您将需要一些外部的东西,比如pyinstaller。
If your question is about where to put configuration/data files, you'll need to work platform-dependently (like writing into the registryor the home folder), as far as I know.
回答by Ed Patrick Tan
For those who are beginners in Python Packaging, I suggest going through this Python Packaging Tutorial.
对于 Python 打包的初学者,我建议阅读这个Python 打包教程。
Note about the tutorial:
教程注意事项:
At this time, this documentation focuses on Python 2.x only, and may not be as applicable to packages targeted to Python 3.x
目前,本文档仅关注 Python 2.x,可能不适用于面向 Python 3.x 的包