How to create Debian package for Python script

Default featured post

Since I have touched Linux, I was wondering how to create binary packages (DEB and RPM), but, I have never had a chance to study about binary packages and how package managers handle them. Until, few months ago that I decided to create a Python script or to be precise weather indicator for Ubuntu Unity and Gnome 3. The time I was developing the indicator, I did Googling and came across with few useful links and managed to make my own Debian package finally and added to the UbuntuIndicatorWeather GitHub repository. In this post, I go through the process of creating Debian package for Python script. Note that, creating Debian package for other types than Python are not covered in this post.

Basically making Debian package at first might look slightly scary but it is quite easy and straight forward. The first thing you need to do is to create a folder named debian in your project directory where your Python script is located. Your directory should look like below,

Project folder
  |------ Python scripts
  |------ Desktop file 
  |------ debian folder

As you can see any other files (such as desktop files) should be placed in the root project directory and shouldn’t be mixed in debian folder.

The next step is to add the following files to your debian directory.

  • changelog
  • compat
  • control
  • copyright
  • install
  • rules

As a result the project tree structure will look like follow,

Project folder
  |------ Python scripts
  |------ Desktop file
  |------ debian folder
  |------ changelog
  |------ compat
  |------ control
  |------ copyright
  |------ install
  |------ rules

Each file does set of task and each has special meaning when a package is constructed. Let’s start with changelog file. Basically in your changelog file you should write information such as changes in your script, version of your package, minimum required system. Following is the changelog of my Python script,

In compat file basically you just need to type letter 7 or 9 in your file and save it, that’s it. According to official Debian guide The compat file defines the debhelper compatibility level. Currently, you should set it to the debhelper v9.

Control file indicates dependencies that your script has and required to be fulfilled to function properly as well as other parameters used by package managers to manage the package. Following is the control file of my script,

Copyright file holds your package copyright related stuff, though, it should follow an especial format like following,

The most important file is install which indicates where your script should be installed. For instance, my script installed in /usr/bin and etc/xdg/autostart. Therefore, my install file is similar to following,

Last but not least is the rules file which is similar to Makefile and will be executed by package manager. The content of this files should be something like below for Python script.

Very well, we have almost done file configuration and now it’s time to create .deb and .tar.gz packages out of the project. To build a Debian package open your console and cd to your project directory. The run this command,

$ dpkg-buildpackage -us -uc

If you have followed the mentioned steps , you should be able to get your desired Debian file which is mostly generated outside of your project directly.

Finally, you can create an alias command to simplify the memorization of the command name.

For more information have a look at the following links,