9

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)?

2 Answers 2

9

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 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

However, you would probably need to provide the full path to the executable as your desktop shell might not show .desktop files which's Exec line cannot be stated. GNOME Shell does that. So I think the question then is: How make setuptools replace the path in the Exec line of the .desktop file it installs?
@FrederickNord: You're right, although, the docs state that Exec may be specified "with the name of the executable only". I think this is strange. As a workaround, the following works fine for me: Exec=bash -c 'export PATH="$PATH:~/.local/bin"; some-executable %U' The benefit is, that the location of some-executable may in user's local tree as well as in the global path.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.