How do I do Debian packaging of a Python package?

Question:

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.

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

The packaging script must use python-support to 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 Jackalope).

Most advice I find while googling are just examples of nasty hacks that don’t even use python-support or python-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?

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

Asked By: fadedbee

||

Answers:

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 source or by visiting http://packages.debian.org.

You may find the following resources of use:

Answered By: derobert

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

 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

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 debian folder; replace all references to flup with your own package name.

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

Answered By: Martin v. Löwis

I think you want 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.

Answered By: Nikratio

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.

Answered By: jpoppe

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

Answered By: user3672754
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.