Skip to content

Instantly share code, notes, and snippets.

@nathakits
Last active August 25, 2023 17:59
Show Gist options
  • Save nathakits/7efb09812902b533999bda6793c5e872 to your computer and use it in GitHub Desktop.
Save nathakits/7efb09812902b533999bda6793c5e872 to your computer and use it in GitHub Desktop.
Creating a `.desktop` file in Ubuntu that launches a Python script

Creating a .desktop file in Ubuntu that launches a Python script

Here we will make two .desktop files that will launch Pupil Capture and Pupil Player.

Steps

  1. Right click on your desktop and create a new empty document.
  2. Set the document file type by Renaming the document that ends with .desktop
  3. Drag the document into a text editor and edit the document with the example code below

Note: Change the X-Icon-Path to the icon you want to use and Execpath to where Pupil Capturemain.py` is located on your machine (see example pupil.desktop for reference)

[Desktop Entry]
Name=Pupil Capture
Version=v0.8.5
Icon=pupil-capture
X-Icon-Path=/path/to/icon/file/
Exec=python /path/to/py/file/main.py
Terminal=false
Type=Application

Option - you can use Terminal=true to launch the terminal if desired.

In the terminal set the permissions of the desktop file to be executable by typing this: chmod u+x path/to/py/file/main.py

Right click on the new empty document and in the Permissions tab, set the execute to allow executing file as program.

[Desktop Entry]
Name=Pupil Capture
Version=v0.8.5
Icon=pupil-capture
X-Icon-Path=/home/pupil-labs/Documents/Repo/pupil/deployment/deploy_capture
Exec=python /home/pupil-labs/Documents/Repo/pupil/pupil_src/capture/main.py
Terminal=false
Type=Application
[Desktop Entry]
Name=Pupil Player
Version=v0.8.5
Icon=pupil-player
X-Icon-Path=/home/pupil-labs/Documents/Repo/pupil/deployment/deploy_player
Exec=python /home/pupil-labs/Documents/Repo/pupil/pupil_src/player/main.py
Terminal=false
Type=Application
@CaptStephan
Copy link

@BramBorg, not sure if you got an answer to this, but I think it would be better to have the Python code ask for the parameters.

@BigBrar
Copy link

BigBrar commented Jul 31, 2022

It didn't work for me. I am using Kali Linux and it didn't work. I marked the file executable but nothing happens when i execute it. I ticked the run in terminal option and then i saw that the terminal just opens for a sec and closes immediately.

@mabdelaal86
Copy link

I have the same problem, but I fixed it with the following steps:

  • Moved the python script file into "~/.local/bin" or any directory listed in PATH env variable.
  • Make sure the file is executable.
  • Call the file directly in the .desktop file, for ex. Exec=my-file.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment