是否有一种标准的方法来创建 Debian 包来分发 Python 程序?

有大量关于如何做到这一点的信息,但是自从 “剥猫皮的方法不止一种”,以及所有的教程/手册,涵盖了一点过程似乎作出了某些假设,这是不同于其他教程,我仍然没有设法掌握它。

到目前为止,我想我明白了。

  1. 我的最终目标应该是创建一个“二进制”。Deb 包裹。这样的包与平台无关(32/64位) ,因为所有 Python 程序都是这样的。
  2. 要创建一个“二进制”包,我首先需要创建一个源包。
  3. 要创建源代码包,我可以使用 CDBSdebhelper
  4. The core of creating a source package is populating the DEBIAN directory in the source directory with a number of files clarifying where files need to be copied, what copyright and licensing scheme they are subject to, what dependencies they have, etc...
  5. 步骤 # 4可以在很大程度上自动化 dh_make命令 如果,Python 源代码也带有 distutils 的 setup.py脚本。

现在我的问题是:

  1. 我对这个过程的理解正确吗? 我是否遗漏了什么,或者我做错了什么?
  2. 第五步对我来说确实更加令人困惑: 具体来说,对我来说最模糊的两点是:
    • 如何编写安装独立程序的 setup.py脚本?独立计划指的是一个用于桌面用户的程序(相对于我所理解的 模组,它是一个功能集合,在导入之后由其他软件使用)。在我的特殊情况下,我实际上需要 这样的“程序”: 主要的软件和一个单独的实用程序(实际上,第二个“程序”应该在同一个包与另一个)。
    • 这种 DEB 包脚本的特性是什么?官方文档似乎只处理 RPM 和 Windows 的东西..。

顺便说一下: 这些是我目前能找到的最好的信息来源。如果你有什么比这更好的,请分享!:)

40604 次浏览

It looks like stdeb will do what you want.

Also, for installing scripts, I strongly recommend distribute's console_scripts entry point support.

This article by Barry Warsaw helped me in getting quite far through the process. I still had to do a lot of searching on the side, though, and I read most of the Ubuntu packaging guide some time in the past.

Having a good setup.py is a really good advice. I found these two guides quite good:

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.

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):

  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 files and 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-Version and your deb package will be created! (You can also add the post/pre inst scripts and everything you want, it works like a normal Debian package)


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

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.

There are several libraries out there which abstract away all the necessary steps and let you transform your Python package into a Debian package with a single command.

Assuming your python package already has the setup.py, in the directory where setup.py is located, you could use:

  • stdeb (Already mentioned in this answer, install with pip install stdeb). To create a Debian package, run:

    python setup.py --command-packages=stdeb.command bdist_deb
    

    The output .deb file will be located in the bdist_deb directory.

  • fpm (install with gem install --no-ri --no-rdoc fpm). To create a Debian package, run:

    fpm -s python -t deb setup.py
    
  • py2deb (install with pip install py2deb). To create a Debian package, run:

    py2deb -r . .
    

Each of these libraries has its own caveats, so you might want to try what works best for you.

This method works for me.

  1. install stdeb. (pip install stdeb)

  2. create setup.py. I'm using PyCharm. This will (https://www.jetbrains.com/help/pycharm/creating-and-running-setup-py.html) write these commands in your project directory.

  3. sudo apt-get install python3-stdeb fakeroot python-all
  4. python3 setup.py sdist
  5. sudo python3 setup.py --command-packages=stdeb.command bdist_deb
  6. your deb file is in the deb_dist directory that is in your project