14

How to run a Python program directly?

I have created a .py file (say, mnik.py) in gedit. It runs smoothly in terminal.

Command goes

python3 mnik.py

But whenever I click on the file it is opened with gedit. I cannot run it directly by clicking.

What to do?

vaultah
  • 105

4 Answers4

28

There's two things needed.

  1. A script must have #! line telling the OS which interpreter to use. In your case your very first line in the code must be #!/usr/bin/env python3
  2. You need to open file manager , go to Edit -> Preferences -> Behavior, and select what to do with executable files

    enter image description here

    1. Finally , make sure your file itself actually has executable permissions set. In terminal you can do chmod +x /path/to/script.py and in GUI, right click on the file and alter its Properties -> Permissions

    enter image description here

    enter image description here

Note about shebang line

The very first line is called shebang line and must start with #! ; whatever comes next is the name of the interpreter that will read your code. In case you are using python3 you could use either #!/usr/bin/python3 or #!/usr/bin/env python3 for portability. If you are not using code that will be specific to python version - just use #!/usr/bin/env python

Note on the script output:

If your script prints output to console, it will need to have terminal window, or alternatively use GUI dialogs such as zenity. Prefer using Run in Terminal option if you want to see the code. If you want the script to do something without seeing console output - use Run option.

enter image description here

In addition, if you have command line parameters , such as sys.argv[1] in the script , you can't set them unless you have terminal window open.

3

You need to put the location of the program to execute your code on the first line and you then need to set the script to run as an executable by changing its permissions. This assumes you're launching your applications from terminal or another script.

Find your Python installation

$ which python
/usr/bin/python

Add the programs location to the top line of your program with a #! in front

#!/usr/bin/python

# Python code goes here....

Set the Python script to have execution rights

$ chmod 700 test.py

Now you can run the script directly

$ ./test.py

Code listing for test.py

#!/usr/bin/python

print "test"
Joseph
  • 133
2

If you want to run this program without typing python3 mnik.py you have to make the script executable and make sure that python3 is used to run it.

The first you can do by running

 chmod +x mnik.py

the second you can do by adding as the first line of a script a shebang line that invokes python3. On all the Ubuntu systems I have worked with that came with python3, you can get python3 by adding this line at the top:

#!/usr/bin/env python3

After those two changes you can type /path/to/mnik.py, ./mnik.py or just mnik.py (the latter requires the script to be in your PATH).

If you make these changes you might also want to consider renaming mnik.py to mnik, that is common practice with Python packages with commands that are published on PyPI.

Anthon
  • 277
0

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:

  1. It doesn't work with scripts that have a space in their name.
  2. 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.
Eliah Kagan
  • 119,640