Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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???

share|improve this question
1  
You probably need the shebang, #!/usr/bin/python at the beginning of the script. –  Sabacon Apr 13 '13 at 20:40
    
Thank U thank U thank U!!!!! I had the file wrong.. the /usr part.. the slash at the beginning was my problem. Thanx Sabacon!!! –  The Dude Apr 13 '13 at 21:12
    
so...if the problem is solved, @Sabacon you should probably post your comment as an answer and The Dude should choose that answer as correct. –  Nil Apr 18 '13 at 22:32
add comment

4 Answers

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

share|improve this answer
add comment

To recreate your issue, I first verified that I had Python installed on my system.

xaap001@ubuntu:~$ which python
/usr/bin/python
xaap001@ubuntu:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

I then took the Python "hello world" code you posted above and saved it to a file called helloWorld.py. I did not alter the permissions on this file after saving it. From terminal, I tried the following:

xaap001@ubuntu:~$ python helloWorld.py
hello world
Enter your NameAaron
heyAaron

Looks like it worked. From terminal, you just need to preface all Python scripts with python.

Edit: I decided to try and get this to work from the desktop, by creating a desktop shortcut as described in this question/answer: Desktop shortcut to create a new desktop shortcut doesn't do anything

I set the "command" to: python /home/xaap001/helloWorld.py

That gave me a desktop icon I could click on. And it did nothing. I checked the actual content of the .desktop file (shortcut):

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_US]=python
Name[en_US]=Python helloWorld
Exec=python /home/xaap001/helloWorld.py
Name=helloWorldPy
Icon=python

I noticed the Terminal=false setting, and changed it to Terminal=true. Then double-clicking on the icon worked.

share|improve this answer
add comment

can U believe that? I have to admit, I think my ubuntu has a virus or something.. but yes, I did that and this:

lucky1@babyCatsDesktop:~/shiite/livescript$ python rev.py

Hello WOrld

Enter your Name Please: Dave

Traceback (most recent call last):

File "rev.py", line 2, in

vary = input("Enter your Name Please:  ")

File "", line 1, in

NameError: name 'Dave' is not defined

lucky1@babyCatsDesktop:~/shiite/livescript$

Yet it works on the python terminal perfectly? I'm not good at this stuff and I seem to find the most unique, yet common problems out there..

share|improve this answer
add comment

In Python 2 (which you are probably using) input() and raw_input() aren't the same. Your script uses input(), unlike the example.

input() takes your input and tries to evaluate it. Because there is no object called "Dave" in your namespace, Python doesn't know what to do. raw_input() however converts your input to a string, this should be used in almost all of the cases.

Note: In Python 3, raw_input() has been renamed to input().

You think nothing happens because an error is thrown and the script terminates. This is also the reason why you should always run scripts from the terminal, so you can see error messages or general debugging information which is printed.

So, change your script to use raw_input() and run it from the terminal until you got all the bugs out, then try double-clicking it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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