How to create a deb package for a python3 script?

6,202

Creating a .deb for a python3 script is very simple, and only requires a few changes in debian/rules and debian/control if you're familiar with python2 packaging.

In a nutshell:

  1. Create the package source dir

    mkdir myscript-0.1
  2. Copy your python3 script (or the sample script below) to the source dir

    cp ~/myscript myscript-0.1
    cd myscript-0.1

    Sample script:

    #!/usr/bin/python3
    
    if __name__ == '__main__':
        print("Hello world")
  3. Create the packaging skeleton (debian/*)

    dh_make -s --indep --createorig
  4. Remove the example files

    rm debian/*.ex debian/*.EX debian/README.*
  5. Edit debian/control

    Replace its content with the following text:

    Source: myscript
    Section: utils
    Priority: optional
    Maintainer: Name, 
    Build-Depends: debhelper (>= 9), python3
    Standards-Version: 3.9.5
    X-Python3-Version: >= 3.2
    
    Package: myscript
    Architecture: all
    Depends: ${misc:Depends}, ${python3:Depends}
    Description: insert up to 60 chars description
     insert long description, indented with spaces
    
  6. debian/install must contain the script to install as well as the target directory

    echo myscript usr/bin > debian/install
  7. Edit debian/rules

    Replace its content with the following text:

    #!/usr/bin/make -f
    
    %:
        dh $@ --with=python3

    Note: it's a TAB before dh $@, not four spaces!

  8. Build the package

    debuild -us -uc

You will get a few Lintian warnings/errors but your package is ready to be used:

../myscript_0.1-1_all.deb
Share:
6,202

Related videos on Youtube

meecoder
Author by

meecoder

I am a programming hobbyist. I like making webpages, sometimes with PHP, and do Python programming. I have been programming since I was 6 or 7, when I used Scratch, a graphical drag and drop programming language. I program on Github as meecoder. I do NOT do professional programming, and all code I create is free to use and completely open source. I am currently learning Java on Udacity.

Updated on September 18, 2022

Comments

  • meecoder
    meecoder over 1 year

    I have one Python file, main.py. I would like to be able to make a .deb package from it, and then be able to run main.py by typing the package name from the terminal. It is written in Python 3, so the package name should run:

    python3 main.py
    

    The only dependency I know of is python3.

    I have tried creating a deb with a dependency of python3, and then running python3 packagename, but I get:

    /usr/bin/python3: can't find '__main__' module in 'packagename'
    

    Trying to use Debreate for package creation fails to open with :

    Traceback (most recent call last):
      File "/usr/bin/debreate", line 12, in <module>
        import wx, sys, os, debreate, db, language, shutil
      File "/usr/share/debreate/debreate.py", line 23, in <module>
        import os, sys, wx.lib.dialogs, db, webbrowser, language, shutil, subprocess
      File "/usr/share/debreate/db.py", line 5, in <module>
        import wx, wx.combo, wx.lib.mixins.listctrl as LC, os, sys, language
    ImportError: No module named combo
    
    • Admin
      Admin over 10 years
    • Admin
      Admin over 10 years
      Aditya: all answers used python2. I need it to use python3.
    • Admin
      Admin over 10 years
      I also need to be able to run the package from the command line. I tried this already but after installation i get command not found
    • Admin
      Admin over 10 years
      I have left a comment below the post of @andrewsomething to update it for Python 3. In the meantime please edit your question to include what you have already tried and what are the results/error that you get.
    • Admin
      Admin about 10 years
      have you tried fpm? FUNDAMENTAL PRINCIPLE: IF FPM IS NOT HELPING YOU MAKE PACKAGES EASILY, THEN THERE IS A BUG IN FPM.
  • Bernmeister
    Bernmeister almost 10 years
    Do you know how to include translations (PO/POT) into the DEB file? I think there's a command needed within debian/rules but I've never been able to figure it out.
  • Sylvain Pineau
    Sylvain Pineau over 9 years
    @Bernmeister, Please create a new question about translation support. I'd prefer to keep this one as simple as possible. Thanks
  • Chris1804505
    Chris1804505 over 3 years
    dh_make: error: argument -i/--indep: not allowed with argument -s/--single