Search code examples
pythonaptlinux-mint

Debianzing a Python program to get a .deb


Aim

To create an installable .deb file (or package). Which when clicked would install the software on a Linux machine and an icon would be put on the GNOME panel. So as to launch this application from there.

What I have referred to

I referred to two debianizing guides.

Guide 1

Guide 2

The first one had a video which was impossible to understand, partly because of the accent and partly because it was hopelessly outdated.(it was uploaded in 2007)

And the second one was completely text. I got till the 4th Step, Builds the package. But when I did it I got output that did not match what was given in the guide.

What I require

I have a simple python program. It takes your age and then prints back out if the age is below, equal to, or above 18 years. There is just one file and no other dependency for this program. And I want to build this into a .deb.

Specs

-Python 2.7

-Linux Mint

Edit

I followed the exact directory structure as you instructed as you. And replaced all myscript with cowsandbulls. The build completed and I got the Debian. When I installed it and then ran the command cowsandbulls from the terminal I got the following error:

Traceback (most recent call last):
  File "/usr/bin/cowsandbulls", line 9, in <module>
    load_entry_point('cowsandbulls==1.0', 'gui_scripts', 'cowsandbulls')()
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 337, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2311, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2017, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named cowsandbulls

Solution

  • I just tested stdeb (see https://pypi.python.org/pypi/stdeb), a Python package for turning any other Python package into a Debian package.

    First I installed stdeb:

    apt-get install python-stdeb
    

    Then I made a simple script called myscript.py with the following contents:

    def main():
        print "Hello world, says myscript!"
        # wait for input from the user
        raw_input()
    
    if __name__ == '__main__':
        main()
    

    Importantly, your directory structure should be:

    somewhere/myscript/
        setup.py
        myscript/
            __init__.py
            myscript.py
    

    In the setup.py file, you do something like:

    import os
    from setuptools import setup
    from nvpy import nvpy
    
    setup(
        name = "myscript",
        version = "1.0",
        author = "Charl P. Botha",
        author_email = "cpbotha@vxlabs.com",
        description = "Demo of packaging a Python script as DEB",
        license = "BSD",
        url = "https://github.com/cpbotha/nvpy",
        packages=['myscript'],
        entry_points = {
            'console_scripts' : ['myscript = myscript.myscript:main']
        },
        data_files = [
            ('share/applications/', ['vxlabs-myscript.desktop'])
        ],
        classifiers=[
            "License :: OSI Approved :: BSD License",
        ],
    )
    

    The console_scripts directive is important, and it'll create an executable script called my_script, which will be available system-wide after you install the resultant DEB. If your script uses something like tkinter or wxpython and has a graphical user interface, you should use gui_scripts instead of console_scripts.

    The data_files directive will install a suitable desktop file into /usr/share/applications, so that you can also start myscript from your desktop environment. vxlabs-myscript.desktop looks like this:

    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=myscript
    Comment=Minimal stdeb example
    # myscript should wait for user input at the end, else the terminal
    # window will disappear immediately.
    Exec=myscript
    Icon=/usr/share/icons/gnome/48x48/apps/file-manager.png
    Categories=Utility;
    # desktop should run this in a terminal application
    Terminal=true
    StartupNotify=true
    StartupWMClass=myscript
    

    To build the DEB, you do the following in the top-level myscript:

    python setup.py --command-packages=stdeb.command bdist_deb
    

    Which will create a .deb in the deb_dist directory.

    After having installed the DEB I created like this, I could run myscript from the command-line, and I could also invoke it from my desktop environment.

    Here's a GitHub repository with the example code above: https://github.com/cpbotha/stdeb-minimal-example