我如何对 Python 包进行 Debian 打包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1382569/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:06:10  来源:igfitidea点击:

How do I do Debian packaging of a Python package?

pythondebian

提问by fadedbee

I need to write, or find, a script to create a Debian package, using package python-support, from a Python package. The Python package will be pure Python without C extensions.

我需要编写或查找脚本以使用 packagepython-support从 Python 包创建 Debian包。Python 包将是没有 C 扩展的纯 Python。

The Python package for testing purposes will just be a directory with an empty __init__.pyfile and a single Python module, package_test.py.

用于测试目的的 Python 包只是一个包含空__init__.py文件和单个 Python 模块package_test.py.

The packaging script mustuse python-supportto provide the correct bytecode for possible multiple installations of Python on a target platform, i.e. v2.5 and v2.6 on Ubuntu 9.04(Jaunty Hymanalope).

打包脚本必须用于python-support为目标平台上可能的多个 Python 安装提供正确的字节码,即Ubuntu 9.04(Jaunty Hymanalope)上的 v2.5 和 v2.6 。

Most advice I find while googling are just examples of nasty hacks that don't even use python-supportor python-central.

我在谷歌搜索时发现的大多数建议只是一些讨厌的黑客攻击的例子,甚至不使用python-supportpython-central

I have spent hours researching this, and the best I can come up with is to hack around the script from an existing open source project, but I don't know which bits are required for what I'm doing.

我花了几个小时研究这个,我能想到的最好的办法是从现有的开源项目中破解脚本,但我不知道我正在做的事情需要哪些位。

Has anyone here made a Debian package out of a Python package in a reasonably non-hacky way?

这里有没有人以一种合理的非 hacky 方式从 Python 包中制作了一个 Debian 包?

I'm starting to think that it will take me more than a week to go from no knowledge of Debian packaging and python-supportto getting a working script. How long has it taken others?

我开始认为从不了解 Debian 打包python-support到获得可运行的脚本需要一个多星期的时间。其他人花了多长时间?

采纳答案by Martin v. L?wis

I would take the sources of an existing Debian package, and replace the actual package in it with your package. To find a list of packages that depend on python-support, do

我会使用现有 Debian 软件包的源代码,并将其中的实际软件包替换为您的软件包。要查找依赖于 python-support 的软件包列表,请执行

 apt-cache rdepends python-support

Pick a package that is Architecture: all, so that it is a pure-Python package. Going through this list, I found that e.g. python-flup might be a good starting point. To get the source of one such package, do

选择一个 是 的包Architecture: all,以便它是一个纯 Python 包。通过这个列表,我发现例如 python-flup 可能是一个很好的起点。要获得一个这样的包的来源,请执行

apt-get source <package>

To build it, do

要构建它,请执行

cd <packagesrc>
dpkg-buildpackage -rfakeroot

When editing it, expect that you only need the files in the debianfolder; replace all references to flup with your own package name.

编辑它时,期望您只需要文件debian夹中的文件;用您自己的包名替换对 flup 的所有引用。

Once you get started, it should take you a day to complete.

一旦开始,您应该需要一天的时间才能完成。

回答by rsm

The right way of building a .deb package is using dpkg-buildpackage, but sometimes it is a little bit complicated. Instead you can use dpkg -b <folder>, and it will create your Debian package.

构建 .deb 包的正确方法是使用dpkg-buildpackage,但有时它有点复杂。相反,您可以使用dpkg -b <folder>,它会创建您的 Debian 软件包。

These are the basics for creating a Debian package with dpkg -b <folder>with any binary or with any kind of script that runs automatically without needing manual compilation (Python, Bash, Perl, and Ruby):

这些是dpkg -b <folder>使用任何二进制文件或任何无需手动编译(Python、Bash、Perl 和 Ruby)自动运行的脚本创建 Debian 软件包的基础知识:

  1. Create the files and folders in order to recreate the following structure:

    ProgramName-Version/
    ProgramName-Version/DEBIAN
    ProgramName-Version/DEBIAN/control
    ProgramName-Version/usr/
    ProgramName-Version/usr/bin/
    ProgramName-Version/usr/bin/your_script
    

    The scripts placed at /usr/bin/are directly called from the terminal. Note that I didn't add an extension to the script. Also you can notice that the structure of the .deb package will be the structure of the program once it's installed. So if you follow this logic, if your program has a single file, you can directly place it under ProgramName-Version/usr/bin/your_script, but if you have multiple files, you should place them under ProgramName-Version/usr/share/ProgramName/all your filesand place only one file under /usr/bin/that will call your scripts from /usr/share/ProgramName/.

  2. Change all the folder permission to root:

    chown root:root -R /path/to/ProgramName-Version
    
  3. Change the script's permissions:

    chmod 0755 /path/to/the/script
    
  4. Finally, you can run: dpkg -b /path/to/the/ProgramName-Versionand your .deb package will be created! (You can also add the post/pre install scripts and everything you want. It works like a normal Debian package.)

  1. 创建文件和文件夹以重新创建以下结构:

    ProgramName-Version/
    ProgramName-Version/DEBIAN
    ProgramName-Version/DEBIAN/control
    ProgramName-Version/usr/
    ProgramName-Version/usr/bin/
    ProgramName-Version/usr/bin/your_script
    

    放置在的脚本/usr/bin/直接从终端调用。请注意,我没有向脚本添加扩展名。您还可以注意到 .deb 包的结构将是程序安装后的结构。所以,如果你按照这个逻辑,如果你的程序有一个单一的文件,你可以直接将其下ProgramName-Version/usr/bin/your_script,但如果你有多个文件,你应该把他们下ProgramName-Version/usr/share/ProgramName/all your files并放置只有一个文件下/usr/bin/,将调用脚本的/usr/share/ProgramName/

  2. 将所有文件夹权限更改为root:

    chown root:root -R /path/to/ProgramName-Version
    
  3. 更改脚本的权限:

    chmod 0755 /path/to/the/script
    
  4. 最后,您可以运行:dpkg -b /path/to/the/ProgramName-Version您的 .deb 包将被创建!(您还可以添加安装后/安装前脚本以及您想要的所有内容。它的工作原理类似于普通的 Debian 软件包。)

Here is an example of the controlfile. You only need to copy-paste it in to an empty file called "control" and put it in the DEBIANfolder.

这是该control文件的示例。您只需要将其复制粘贴到一个名为“control”的空文件中,然后将其放入DEBIAN文件夹中。

Package: ProgramName
Version: VERSION
Architecture: all
Maintainer: YOUR NAME <EMAIL>
Depends: python2.7, etc , etc,
Installed-Size: in_kb
Homepage: http://example.com
Description: Here you can put a one line description. This is the short Description.
 Here you put the long description, indented by one space.


The full article about Debian packages can be read here.

可以在此处阅读有关 Debian 软件包的完整文章。

回答by Nikratio

I think you want http://pypi.python.org/pypi/stdeb:

我想你想要http://pypi.python.org/pypi/stdeb

stdeb produces Debian source packages from Python packages via a new distutils command, sdist_dsc. Automatic defaults are provided for the Debian package, but many aspects of the resulting package can be customized (see the customizing section, below). An additional command, bdist_deb, creates a Debian binary package, a .deb file.

stdeb 通过新的 distutils 命令 sdist_dsc 从 Python 包生成 Debian 源包。Debian 软件包提供了自动默认值,但生成的软件包的许多方面都可以自定义(请参阅下面的自定义部分)。另一个命令 bdist_deb 创建一个 Debian 二进制包,一个 .deb 文件。

回答by jpoppe

Most of the answers posted here are outdated, but fortunately a great Debian wiki post has been made recently, which explains the current best practices and describes how to build Debian packages for Python modules and applications.

此处发布的大多数答案都已过时,但幸运的是,最近发布了一篇很棒的 Debian wiki 帖子,其中解释了当前的最佳实践并描述了如何为 Python 模块和应用程序构建 Debian 包。

回答by derobert

First off, there are plenty of Python packages already in Debian; you can download the source (including all the packaging) for any of them either using apt-get sourceor by visiting http://packages.debian.org.

首先,Debian 中已经有很多 Python 包;您可以使用apt-get source或访问http://packages.debian.org下载其中任何一个的源代码(包括所有打包)。

You may find the following resources of use:

您可能会找到以下使用资源: