13

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?

  • 2
    Related: How do I run Python code as a script? – jfs Apr 23 '16 at 18:05
  • 4
    @kasperd Typing 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 like mercurial, sphinx). Care to explain why what I have been doing for 20+ years is not the proper way? – Anthon Apr 24 '16 at 7:58
  • 3
    @kasperd A correct written Python script (or program) doesn't have to have a shebang line. If it doesn't it is still a Python script in every way. That script just cannot be executed by from the Linux shells in the short-hand form you seem to prefer. There are also environments that don't require Python scripts to have a shebang line at all and allow them to start the script by double clicking. – Anthon Apr 24 '16 at 9:20
  • 1
    @kasperd What the kernel needs in order for a Python script to run using ./scriptname doesn't validate your claim that running a script from the commandline by using python3. There are different ways of running python scripts and doing python3 ./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 9:34
  • 1
    Using <language> <script> is a totally valid and accepted way to run a script. Suggesting it is bad practice should be frowned on. – Jacob Vlijm Apr 24 '16 at 9:54
26

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.

  • 1
    @PriyadarshiPaul That's the same as Files program on the launcher . In windows-speak that would be file explorer or My Computer – Sergiy Kolodyazhnyy Apr 23 '16 at 17:46
  • @PriyadarshiPaul it's nautilus in the Terminal – grooveplex Apr 23 '16 at 17:47
  • 1
  • 3
    #!/usr/bin/env python will invoke Python2 on most Ubuntu systems. Although you can make a Python script written for Python3 compatible with Python2, I would not assume that the OP has done so. #!/usr/bin/env python3 seems a more appropriate shebang – Anthon Apr 24 '16 at 9:22
  • 1
    And why are we bringing up Arch Linux here ? Maybe I'll come across as a bit mean , but we're talking Ubuntu here. On Ubuntu by default python is linked to python2.7 If you're developing software , you probably should be aware of what links where, if you use something other than your OS's default version, then explicitly specify so. Sure , we can cater to each individual question , but for crying out loud - if you code , then do it right . – Sergiy Kolodyazhnyy Apr 24 '16 at 17:55
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"
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.

  • ok , Any of these now does not work – Priyadarshi Paul Oct 5 '16 at 18:35
  • What is "ok"? What does "these" refer to? – Anthon Oct 5 '16 at 18:41
  • It's saying ** usr/bin/env: bad interpreter: No such file or directory ** – Priyadarshi Paul Oct 5 '16 at 19:20
  • There is at least a / missing at the front there. Please explain what "It" is in you comment. Is it the bash shell? Is it windows Command.com? From what are you invoking the program and with what commandline syntax? – Anthon Oct 5 '16 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.