I have found a workaround if you don't want to bother with setting your script as an executable, adding comments to your code or selecting to run from the terminal every time you run it.
Go to ~/.local/share/applications
, create a new .desktop
file there. Call it something like python-run.desktop
. Paste the following into it:
[Desktop Entry]
Name=Run Python Script
Comment=Python Interpreter
Exec=gnome-terminal -- /bin/bash -c 'python3 %f;echo "$(tput setaf 1)Program terminated.\nPress enter to exit...$(tput sgr 0)"; read'
Icon=/usr/share/pixmaps/python3.6.xpm
Terminal=true
Type=Application
Categories=Development;
StartupNotify=true
NoDisplay=true
This is mostly copied from the .desktop
file of python interpreter itself. The difference is when you open it, it runs a new instance of the terminal line with the command: python3 %f;echo "$(tput setaf 1)Program terminated.\nPress enter to exit...$(tput sgr 0)"; read'
, which runs the script (%f
is apparently the file's path), then pauses on exit.
Then go to nautilus, right click on the script, go to Properties → Open With and select Run Python Script, the "Application" we just created. Now when you double click, it should run the script from the terminal.
It's a pretty good workaround but I have found 2 problems with it:
- It doesn't work with scripts that have a space in their name.
- It opens a separate terminal window which is pretty annoying. For some reason, I couldn't get it to work by just setting
Exec=python3 %f
, it kept giving me an end of file exception whenever the program tried to get input. No idea why.
python3
on the command line with an argument is an excellent way to run python scripts. It is in fact the easiest way to run a program that requires a terminal and arguments (like many of my own scripts, but also programs likemercurial
,sphinx
). Care to explain why what I have been doing for 20+ years is not the proper way?./scriptname
doesn't validate your claim that running a script from the commandline by usingpython3
. There are different ways of running python scripts and doingpython3 ./mnik.py
is also a proper way of doing things, it is just different (and at least you get python3 that way and not python2 as is more likely with the most the answers so far)