I am new to both Python and Linux and as such request simple explanations with minimal assumed knowledge where possible please, however I am more than willing to invest time and effort to learn.
I have a Raspberry Pi 2 (Model B V1.1) that is running Linux. I interact with this pi via putty.
I am trying to create a simple competitive reflex game, consisting of 2 buttons and a single LED. My goal is to have the LED light up after a short interval, and the first player to press their button wins.
I am writing the script for this with python (specifically 2.7.3)
My issue is that i am unable to run ANY .py file from within putty, i always receive the same error:
Syntax error: word unexpected (expecting ")")
To determine if the issue was an error in my code, i created a very very simple .py file, to check if the same error occurs, and it did. So i currently believe even if my code was functional, something is stopping me from running ANY .py file.
The process I am using is as follows:
First I create a new python file from within putty:
sudo nano test.py
Next I enter my python code (very simple for now, as i cannot get ANY .py file to run)
for each in range(5): print 'hello'
I then press CTRL + O to write the file, hit enter, then CTRL + X to exit
Finally, I make the file executable using
sudo chmod u+x test.py
and try to run it
sudo ./test.py
again, a similar error occurs
Syntax error: "(" unexpected
I then decided to enter the code directly into the python shell, using
sudo python >>>for each in range(5): ... print 'hello'
This time the output is the desired outcome:
hello hello hello hello hello
So there is no problem in executing python code directly from the shell, I am just unable to execute any previously saved .py file
Any insight into what could be causing this is much appreciated, and I apologise if I have not provided enough information to be useful for you.
Thanks in advance!
Advertisement
Answer
Short answer: Either run these as python filename.py
, or else add the line #!/usr/bin/python
to the top of your Python scripts.
Long answer: When you run a file from the command line in Linux (which is what the Raspberry Pi is running), by default it assumes that the file is a shell script file (usually Bash script). So it uses the Bash shell (or some other shell, but it’s usually Bash) to interpret the file, and Bash doesn’t know Python syntax. If you want to run your file using a different interpreter (Python, in this case), you have to add a “magic line” at the top of the file starting with #!
(usually pronounced “hash-bang”, and sometimes pronounced “shebang” for short). Following the #!
characters is the full path of the interpreter to use, e.g. /usr/bin/python
for Python scripts. (You can also use /usr/bin/env python
as another answer suggested; I prefer /usr/bin/python
because it’s impossible to get the wrong Python interpreter that way. But that’s getting into advanced topics that may be more than you need right now.)
So when you put the line #!/usr/bin/python
at the top of your Python scripts, you’re telling the Linux system which interpreter to run the program with, and then it should All Just Workâ˘.
Also, STOP using sudo to edit and run these! That’s just asking for trouble.