如何打包命令行Python脚本

时间:2022-12-14 00:11:18

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.py with the key configurations noted below:

我最终使用了setup.py,其中包含以下关键配置:

setup(
....
    entry_points="""
[console_scripts]
mycommand = mypackage.mymodule:main
""",
....
)

Here's a good example in context.

这是上下文中的一个很好的例子。

5 个解决方案

#1


3  

@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.

@Zach,鉴于你对@ soulmerge的回答你的评论澄清,看起来你需要的是按照关于distutils的说明写一个setup.py - 这里特别是你如何在pypi上注册,以及如何一旦你被注册,就上传到pypi - 并且可能(如果你需要一些额外的功能,以及distutils自己提供的功能)添加setuptools,其中easy_install是其中的一部分,通过这里的说明。

#2


4  

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非标准的继续进行方式,而是可以使用scripts参数直接依赖distutils setup的函数,如下所述: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作为外部依赖。

#3


2  

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.

包装是什么意思?如果它是一个在已经安装了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 registry or the home folder), as far as I know.

如果您的问题是关于将配置/数据文件放在何处,那么就我所知,您需要以平台为依据(例如写入注册表或主文件夹)。

#4


2  

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,entry_points字典中的console_scripts键,...),它适用于Python 2和3。

#5


0  

For those who are beginners in Python Packaging, I suggest going through this Python Packaging Tutorial.

对于Python Packaging的初学者,我建议您阅读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的软件包

#1


3  

@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.

@Zach,鉴于你对@ soulmerge的回答你的评论澄清,看起来你需要的是按照关于distutils的说明写一个setup.py - 这里特别是你如何在pypi上注册,以及如何一旦你被注册,就上传到pypi - 并且可能(如果你需要一些额外的功能,以及distutils自己提供的功能)添加setuptools,其中easy_install是其中的一部分,通过这里的说明。

#2


4  

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非标准的继续进行方式,而是可以使用scripts参数直接依赖distutils setup的函数,如下所述: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作为外部依赖。

#3


2  

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.

包装是什么意思?如果它是一个在已经安装了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 registry or the home folder), as far as I know.

如果您的问题是关于将配置/数据文件放在何处,那么就我所知,您需要以平台为依据(例如写入注册表或主文件夹)。

#4


2  

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,entry_points字典中的console_scripts键,...),它适用于Python 2和3。

#5


0  

For those who are beginners in Python Packaging, I suggest going through this Python Packaging Tutorial.

对于Python Packaging的初学者,我建议您阅读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的软件包