I need help running python in ubuntu

I can run python without a problem in my terminal but when I create a .py file I can't get it to run.

  • 1
    So how are you trying? – Jacob Vlijm Feb 26 '15 at 22:37
  • thats what i try to find out – Bitcoin Austria Feb 26 '15 at 22:40
  • There are many ways, depending on if the file is executable or not, with shebang or not. Will see if I can quickly find a dupe with an overview. – Jacob Vlijm Feb 26 '15 at 22:42
  • Follow this answer: stackoverflow.com/a/304896 This will also make you program double-click-able. – MadMike Feb 26 '15 at 22:49
  • whats shebang?? – Bitcoin Austria Feb 26 '15 at 22:52

How it works (examples for python2, for python3, replace all occurrences of python by python3):

  1. python /path/to/script.py
    • works if file is either executable or not
    • shebang (#!/usr/bin/env python) in the head of the script is good practice, but not needed
  2. /path/to/script.py
    • works if script is executable
    • shebang is needed (#!/usr/bin/env python)
  3. script.py
    • works if script is in $PATH
    • script needs to be executable
    • filename needs to have extension
    • shebang is needed (#!/usr/bin/env python)
  4. script
    • works if script is in $PATH
    • script needs to be executable
    • filename should have no extension
    • shebang is needed (#!/usr/bin/env python)

A bit more information: precedence of command, shebang or extension?

The shell can get its information on how to run a script from two sources (in order of precedence):

  • The language information in the command: python <script>
  • The shebang, in the first line of the script: #!/usr/bin/env python

The language extension however does not play a role(!).

A few examples:

  1. A bash script, correct (language) information in the command, incorrect shebang, incorrect extension:

    #!/usr/bin/env python
    echo 'Monkey eats banana'

    ran with:

    $ sh /path/to/script.py
    > Monkey eats banana

    runs correctly, the information in the command takes precedence over both the shebang and the extension.

  2. An (executable) python script, ran with incorrect extension, but a correct shebang:

    print "Monkey eats banana"

    ran with:

    $ /path/to/script.sh
    > Monkey eats banana

    runs correctly, the information in the shebang takes precedence over the extension.

  3. An (executable) python script, with a correct extension, but without shebang (and without language information in the command):

    print "Monkey"

    Ran with the command:

    $ /path/to/script.py
    > Error: no such file "Monkey"

    does not run correctly, in spite of the language extension!


To run a python script, use python:

python script.py
  • i created a small script with sublime and saved it as test.py if i try to run it in the terminal python test.py I get a message that it isnt found test.py is saved on the desktop – Bitcoin Austria Feb 26 '15 at 22:42
  • 1
    @BitcoinAustria Because most likely terminal's current location is not ~/Desktop, change its location to ~/Desktop: in a terminal: cd ~/Desktop – kos Feb 26 '15 at 22:45

Probably you need to make it executable.

chmod +x /path/to/your/file.py

This question is answered here: Running Python File in Terminal
If none of these work:

You can save your code as a script in gedit with a .py file extension ie.exampleFile.py

But on the first line add #!/usr/bin/python and save.

Make the script executable by entering

chmod +x exampleFile.py 

into the terminal.

(I usually save scripts in a separate directory and call the directory in the terminal via cd exampleDirectory and then execute scripts in that specific directory)
[also you can check if the script is in your directory by entering the l command in the terminal to list the files in that directory and the script should be green if it executable]

Then you can call the script to be ran in the terminal:

python exampleFile.py

You can replace python with python3 in all the areas that python was listed, if you'd rather prefer that version

In addition, you can use vim or other third party software to write and run python scripts if none of these work.

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