Packaging a python app for Ubuntu using dh-virtualenv


For a while I've been trying to find the best way to package a python app as a debian file that I can distribute for installing on Ubuntu. 

I also didn't necessarily want to rely on the python packages that are distributed with the system because with python you tend to get used to having the latest packages installed through pip

Then I found dh-virtualenv which is a very neat way of packaging python into debs with the key advantage that these apps are packaged as a virtualenv so that the latest packages can be included without relying on the packages on the system... But system packages can also be used. 

In this post I've created an that example illustrates how to package a python script as a .deb using dh-virtualenv. The example can be found on github.

This includes all the files necessary to package the example as a deb, where the app we are building into a python package is contained in a single file:

       ExImView/image_app.py

This is a basic example of an image viewing app built with PySide/Qt (shown in a previous post)

Setup for ubuntu 14.04

A few packages are required for building packages (let me know if more are required)
  • Install the python development packages

	       sudo apt-get install python-dev git

  • Create a virtualenv (so that the latest pip and setuptools can be install and activate)

	      virtualenv virt-example 
      source ~/virt-example/bin/activate

Build the example debian package of the GUI

Download the example

	     git clone https://github.com/benjaminirving/python-debian-packaging-example.git 
     cd python-debian-packaging-example
In the folder containing the source run
		
			 dpkg-buildpackage -us -uc -b
		
	
This should build a debian outside the folder that can be installed

Screenshots

The newly created debian package can be installed as you would any other package
[Obviously this is just an illustrative example so make sure that the installed files don't overwrite any existing applications. But I wouldn't expect anything to be called eximview or eximview2]


The package now show up in search, can be added to the dock, and includes an icon





Explanations of the packaging files

The key to packaging are the files in the debian folder, which is where the debian packaging and dh-virtualenv settings are contained. 
The dh-virtualenv documentation does a very good job at explaining the following files:

debian/compat
debian/control
debian/changelog
debian/rules
debian/«pkgname».triggers

I'll explain the following files that turn this package into a GUI script

debian/ExImView.desktop
This file specifies the desktop entry once installed on the system. Modify appropriately. 


debian/install
This script is used to copy the .desktop entry, the icon and a bask script to run the app into the appropriate system folders.


debian/rules


eximview2
This is the script that is copied to usr/bin and basically runs the app in the virtualenv that has been created by the deb install.


setup.py
A standard setup.py file is also used. At the bottom of the script an entry point is added, which creates an executable in the virtualenv that is called by the script copied to /usr/bin. This is the best way I could think to link it but there might be better options. 


Notes

Note that this example doesn't take advantage of the main use of dh-virtualenv, which is packaging the latest dependencies with the app. But this can be done trivially by adding them to the requirements.txt file as explained in the dh-virtualenv documentation.

Please suggest changes that improve this example

Related Posts

The move towards library agnostic machine learning

Introduction to Keras Autoencoders

Live tracking of python 3 usage based on a small set of scientific libraries

Thoughts on web tools for visualisation in medical image analysis research

Drag and drop files into a GUI using python and pyside

Interactive visualisations for research papers

Passing numpy arrays between python and c++ using cython

Jabref gtk theme

Rendering volumes on the web with x3dom