How to create a deb package for a python3 script?

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