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 is not the proper way to run a Python script. You should be invoking it as./mnik.py
or if it is in yourPATH
you could simply typemnik.py
. Apart from that, there is an excellent answer below detailing the rest you need to do. – kasperd Apr 23 '16 at 21:37python3
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? – Anthon Apr 24 '16 at 07:58#!
line at the start specifying which interpreter to use. When you type the interpreter name on the command line you might not notice if you made a mistake in creating the script. It also means there is a risk you mistakenly type the incorrect interpreter, which is a risk there is no reason to take. Finally it is shorter to type./mnik.py
than to typepython3 mnik.py
. – kasperd Apr 24 '16 at 09:03#!
line. If you typestrace ./mnik.py
and the script has no#!
line, you will see the error. The error code isENOEXEC
, which translates toExec format error
. There are workarounds to run a badly formatted script, but you should not rely on workarounds, when you have the option to do things right. – kasperd Apr 24 '16 at 09:26print "Hello world"
. This may work just fine if you always use the same workaround. But sooner or later somebody will not know about your preferred workaround, and will try to execute the script the way a script is intended to be executed. It happens to be the case thatbash
has its own workaround, which is different from yours. And the error I get is this:Warning: unknown mime-type for "Hello world" -- using "application/octet-stream"
Error: no such file "Hello world"
– kasperd Apr 24 '16 at 09:34./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) – Anthon Apr 24 '16 at 09:34