How to make Python file run by double-clicking on it in Ubuntu 20.04?
I make a game in Python with the turtle
module but I want to make it executable by double clicking on it. I follow these steps:
-
My Python file has
#!/usr/bin/env python3
on top. -
I created a file called
launcher.desktop
on the desktop. It contains:[Desktop Entry] Name = Pong Exec = /home/username/Desktop/MyGame/game.py Version = 1.0 Icon = /home/username/Desktop/MyGame/game.icon Type = Application
-
I marked the file as executable.
However, when I double-click on that file, it opens in text editor.
Can someone tell me how can I make the Python file executable by double-clicking in Ubuntu 20.04?
Your desktop file is basically correct, double clicking it will be interpreted as "edit" - you can see it in the properties - the default behavior of an icon on a desktop will be "Text editor".
I'd put it into ~/.local/share/applications
. Then you could start it from the activities.
Make sure the exec expression is executable. So your "game.py" must either be executable (then it needs the shebang #!/usr/bin/env python3
in its first line) or the exec must look like:
Exec= /usr/bin/python3 /path/to/mygames/game.py
Note that it would be good style to have the follwing line as first line in your desktop file:
#!/usr/bin/env xdg-open
And the "icon" should be rather *.png than ".icon".
So I called the game "pong":
[]
from where you could put it to your "favorites"
This is the complete desktop file as shown above:
#!/usr/bin/env xdg-open
[Desktop Entry]
Encoding=UTF-8
Name = Pong
#Exec = /usr/bin/python3 /home/username/Desktop/games/game.py
Exec = /home/username/Desktop/games/game.py
Version = 1.0
Icon = /home/username/Desktop/games/game.png
Type = Application
NoDisplay=false
Categories=Games