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.