如何打包命令行Python脚本

发布于 2024-07-26 17:54:57 字数 519 浏览 10 评论 0原文

我创建了一个旨在从命令行使用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

内心旳酸楚 2024-08-02 17:54:57

可以使用 scripts 参数直接依赖 distutils setup 函数,而不是使用 setuptools 非标准方式进行操作,如下所述:http://docs.python.org/distutils/setupscript.html#installing-scripts

from distutils import setup
setup(
    ...,
    scripts=['path/to/your/script',],
    ...
)

它允许你可以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 the scripts argument, as stated here: 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.

窗影残 2024-08-02 17:54:57

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

墨落成白 2024-08-02 17:54:57

上个月,我写了一篇文章来准确回答你的问题。 您可以在这里找到它: 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 the entry_points dictionary, ...), which work for Python 2 and 3.

煮茶煮酒煮时光 2024-08-02 17:54:57

你说的包装是什么意思? 如果它是在已安装 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.

习ぎ惯性依靠 2024-08-02 17:54:57

对于那些 Python 打包的初学者,我建议阅读这个 Python 打包教程

关于教程的注意事项:

目前,本文档仅关注 Python 2.x,可能不适用于针对 Python 3.x 的包

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文