Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install .desktop file with setup.py

Tags:

python

linux

pip

I have a python application that is supposed to be launchable via GUI so it has to have a .desktop file in /usr/share/applications/. The application only supports Linux. Normally, pip installs all files in one directory but it is possible to specify other locations (e.g. the .desktop file) in the setup.py using data_files=[].

Is this considered to be good a solution in this case or is this something that should only happen in a distribution specific package (like .rpm/.deb/.ebuild)?

like image 248
elya5 Avatar asked Aug 13 '14 11:08

elya5


2 Answers

Yes, you can define the install path of your .desktop in your setup.py script.

You can do that because you know where you want to install it, it is not distribution specific. It's loosely defined by the Freedesktop Specififications.

Usually, the desktop files are located in these directories:

  • /usr/share/applications
  • /usr/local/share/applications
  • ~/.local/share/applications

There is not really any reference stating that, but if you want to know more you can have a look at the Desktop Entry Specification and the XDG Base Directory Specification.

So you know that you want to install in share/applications. But what about the prefix before that? The answer is that you, as a developer, don't have to care about that. It's up to the packager to decide.

So, here's how you do it in your setup.py.

from setuptools import setup

setup(
    name       = 'myapplication',
    version    = '0.1',
    packages   = ['myapplication'],
    data_files = [
        ('share/applications', ['data/org.myapplication.desktop']),
    ],
)

As you can see, the directory we give (share/applications) is relative. Quoting Pythons's writing setup script:

If directory is a relative path, it is interpreted relative to the installation prefix (Python’s sys.prefix for pure-Python packages, sys.exec_prefix for packages that contain extension modules).

Thanks to that, when the packager sets the installation prefix (usually /usr or /usr/local), your desktop file will be installed in the correct location.


BTW, talking about desktop files, be sure to read the part about the naming conventions:

... should follow the "reverse DNS" convention, e.g. org.example.FooViewer.desktop.

BTW2, contrary to what has been said on this page, there's no need for an absolute path in the Exec line of your file. Look at the files installed on your system, almost nobody do that.

grep Exec= /usr/share/applications/*.desktop
like image 53
elboulangero Avatar answered Sep 21 '22 00:09

elboulangero


This sounds to me like a good approach but perhaps instead of placing the .desktop file in the system wide /usr/share/applications/ folder, you could place the file in the users applications folder at ~/.local/share/applications.

This would also not require elevated permissions to access the root owned /user directory and it's sub-directories.

like image 33
Lix Avatar answered Sep 20 '22 00:09

Lix