can't run simple program in terminal

Asked by Brian Renshaw

A raw python recruit, I used the IDLE facility in Ubuntu and got the window with the info at the top telling me which version etc, followed by the python prompt. Since I couldn't seem to delete the info about the Python version, I just typed the print instruction thus:

Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> print "Where am I?"
Where am I?
>>>

I saved this as hello.py in a folder I called My_Python, went back to the terminal window, changed to the directory where I'd put the program and then typed the command to run, thus:

brian@ubuntu:~/Desktop/My_Python$ python ./hello.py

the response I got was:

  File "./hello.py", line 1
    Python 2.7.3 (default, Aug 1 2012, 05:14:39)
             ^
SyntaxError: invalid syntax

It looks as if I need to get rid of the stuff that's not proper code (i.e. the Python 2.7.3 etc).
How do I do this?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Brian Renshaw
Solved:
Last query:
Last reply:
Revision history for this message
Hamish McIntyre-Bhatty (hamishmb) said :
#1

Try this:

Check that the first line reads as : #!/usr/bin/python.

Then comment out the next three lines with hashes:

#Python 2.7.3 (default, Aug 1 2012, 05:14:39)
#[GCC 4.6.3] on linux2
#Type "copyright", "credits" or "license()" for more information.

Hope this helps

Revision history for this message
Warren Hill (warren-hill) said :
#2

I think I can see the problem here

If I type "python" without the quotes on my machine I see.

-------------------------------------------

warren@min:~$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

-------------------------------------------------
This is python in interactive mode you can enter commands to try things out here for example if I type
print "Hello"
it now displays
------------------------------------------------------
warren@min:~$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello"
Hello
>>>

---------------------------------------------------------

This is not the way most people use python however. Most people create a script file and run that.

For example I created "hello.py" that contains just two lines

-----------------------------
#!/usr/bin/python
print "Hello"
-----------------------------

The first line tells the command interpreter to run the script through the interpreter that can be found at
"/usr/bin/python" which is where the python interpreter is by default on all Linux systems .

The other line, in this case other line, is the program

To run this program you need to make it executable first

chmod +x hello.py

and you run it with

./hello.py

Hope this helps

Revision history for this message
Brian Renshaw (renshaw-brian) said :
#3

Wonderful!
Thanks Hamish and Warren. Things are becoming clearer.
Interestingly, the mists began to clear when I clicked on the .py file and it opened in gedit! I then found I could edit it as I pleased and get rid of the unwanted text. But what about the #!/usr/bin/python?
I found that this was not necessary if I used the command python ./hello.py -presumably because it runs through the python interpreter? I'm a raw beginner in ubuntu as well but I presume the chmod +x turns it into an executable (.exe?) file?
I did try Warren's method of course, and it worked well. Perhaps it would execute a bit faster?
So where do I look for the executable file? Is that the one with a cogwheel icon that has turned up in my home directory?
Strewth, this turned out to be more complicated than the "tutorial" led me to believe.
Good though. Thanks again mates.

Revision history for this message
Brian Renshaw (renshaw-brian) said :
#4

Incidentally, if you open IDLE and then select 'new file', a blank window opens and you can type away as if you were in gedit, save it as a .py file and all is well.

Revision history for this message
Warren Hill (warren-hill) said :
#5

If a text file is marked as executable

chmod +x filename

Is interpreted by the shell without needing to enter for example "python ./hello.py" you can just type "./hello.py"

The first line tells the shell where to find the interpreter

#!/usr/bin/python

The "#!" is a special comment and must be the first two characters in the file.

You can remove the line if you wish but if you do

./hello.py

will not work

python ./hello

will still work however

Python is an interpreted language so there isn't a seperate "exe" file. Marking the file executable just tells Linux that you can run this script.

Arguably doing it my way is slightly slower as the computer has to

1. Read the file to see which interpreter to use

2. Load the interpreter

3. Pass the file to the interpreter.

python ./hello.py

removes step 1 from the list but the time saved is miniscule. The time it takes you to type python is several magnitudes slower than the extra step.

Revision history for this message
Brian Renshaw (renshaw-brian) said :
#6

Ah, so, the shell is pointed to an interpreter either by a "python" inserted in the command line or by the special comment in the program file, and there is no .exe file. Got it.
So what's this "hello" file (with no extension) that has a cogwheel icon and has appeared in my home directory?

Revision history for this message
Warren Hill (warren-hill) said :
#7

Linux does not rely on extensions it could be the python file you created with IDLE. If you did not specify an extension it may not of given it one