Ubuntu: how to create a .desktop file to launch a python script


I made my litle python App, but now I can't launch the app from the .desktop that I made:

[Desktop Entry]  Name=MyApp  Version=0.1  Exec=/usr/share/MyApp/MyApp.py  Icon=/usr/share/MyApp/img/MyApp.png  Comment=Descriton......  Type=Application  Terminal=false  StartupNotify=false  Categories=Video;GTK;GNOME  

the /usr/share/MyApp folder I made as Root, the MyApp.py has a executable properties

every time I Doble Click the MyApp.desktop the MyApp.py launch, display a systray icon, but it closed and make ubuntu display a error message.

if I run the MyApp.py from Terminal or DClick on the .py file it runs normally, no crashes


Try following text in your .desktop file.

[Desktop Entry]  Version=1.0  Name=ProgramName  Comment=This is my comment  Exec=/usr/share/MyApp/MyApp.py  Icon=/usr/share/MyApp/MyApp.py  Path=/usr/share/MyApp/  Terminal=false  Type=Application  Categories=Utility;Application;  


May I offer a few notes and improvements over the accepted answer?

  • Manually installing software to /usr/share is strongly discouraged!!! That tree should be reserved for software installed by your package manager (Ubuntu Software Center, apt, etc). There is usr/local/share for that. Or, if you don't want to use sudo, you may install for your user only at ~/.local/share. See https://askubuntu.com/a/135679/11015 for more information about software install directories.

  • If you want to execute a .desktop file by double-clicking on it, in Nautilus or your Desktop, just make the .desktop file executable! Note that this is not required if you copy it to /usr/share/applications and launch it via Dash/Menu. Or, better: copy to ~/.local/share/applications or at least /usr/local/share/applications, as suggested above.

  • Icon=/usr/share/MyApp/MyApp.py makes no sense: a .py file is not a valid image, so it cannot be used as an icon. If you want to use the default python icon, use /usr/share/pixmaps/python.xpm

  • The main difference between your .desktop file and the accepted answer is the Path=/usr/share/MyApp/ statement. If that made your app work, it means your software requires the current directory to be the application directory. And that is a bad thing: your software should be able to run fine and find its data files regardless of current directory. (hint: use python's __file__)

  • If your application is a GUI (ie, it has a window), then add StartupNotify=true. It will help the launcher identify its window when it is running.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »