Ubuntu – Can I run a python script in ubuntu 12.04’s from the graphical interface


I'm trying to figure out how to select and run a python file through the ubuntu windows system. I've never run scripts in my life and I'm very new to this.. So I went to youtube cause I felt like this shouldn't have been hard but…

Using the script (and instructions) described here: Create, Save and Run Python Program via Ubuntu Terminal

print"hello world"
vary = raw_input("Enter your Name")
print ("hey" + vary)

I tried to open the the file by double clicking it (again and again)… and it failed, would not ask for my input, just opened the terminal and disappeared (closed the shell) and that's all.. but IDLE was able to run it perfectly so I know it's not the script. When I used VIM to create it as a file and run it from there, I got this output:

Traceback (most recent call last):
  File "shiite/livescript/rev.py", line 2, in <module>
    vary = input("Enter your Name Bitch:  ")
  File "<string>", line 1, in <module>
NameError: name 'Dave' is not defined

One more thing, I've also tried selecting the properties of the file with right click and checked the box that let's it execute.. still nothing..

Anyone know why the Ubuntu terminal won't run python programs??? Maybe there's a terminal that will run something like this???

Best Answer

In Windows, the file types are identified by the last 3 chars after the . However, in linux (and in Ubuntu) .py just helps us humans figure out what kind of file it is. You have to tell Ubuntu that the script you wrote is a python script and it should be run with a python interpreter. You can do this in two ways:

First you can call python followed by the name of the script. That is open a terminal and type:

python hellowworld.py

This will work.

Second, however, you want to use the GUI and run it by double clicking. To this you need a line in the script that tells Ubuntu that the next lines are to be interpreted as a python script. This first line is called the shabang line. For python it looks like:

#! /usr/bin/python

Note the #! at the beginning. The first two characters of your script must be those two followed by the rest of the line. So your script will look like:

#! /usr/bin/python

print"hello world"
vary = raw_input("Enter your Name")
print ("hey " + vary)

As you noted the script must be tagged as executable. To do that, right click on the script's icon and go to Properties>Permissions and check Allow executing file as program.

Now when you double click on the script it will show you a Window with the choices:

Run in terminal, Display, Cancel, Run

You can try Run in terminal. It will open a terminal and show you the prompt:

Enter your name

When you enter your name the terminal will close as the script completes. So you will never see "hey [your name]" as it will happen too fast for your eyes to catch it. You will have to modify the script so that it pauses and wait for you to dismiss it after you see the display.

Hope this helps