Failed to start .py file from putty, Syntax error: word unexpectedly (expected ")")

I am new to both Python and Linux, and as such request simple explanations with minimal supposed knowledge, where possible, please, however I am more than willing to spend the time and effort to study.

I have a raspberry Pi 2 (Model B V1.1) that runs Linux. I interact with this pi through putty.

I am trying to create a simple competing reflex game consisting of two buttons and one LED. My goal is for the LED to light up after a short period of time, and the first player to press his button will win.

I am writing a script for this with python (specifically 2.7.3)

My problem is that I cannot run ANY .py file from putty, I always get the same error:

Syntax error: word unexpected (expecting ")")

To determine if the problem was an error in my code, I created a very simple .py file to check if the same error occurred and it happened. Therefore, I currently believe that even if my code was functional, something is stopping me from running ANY .py file.

The process I use is as follows:

First I create a new python file from putty:

sudo nano test.py

Next, I introduce my Python code (very simple, since I cannot get ANY .py file to run)

for each in range(5):
    print 'hello'

Then I press CTRL + O to write the file, press enter, then CTRL + X to exit

Finally, I am making an executable using

sudo chmod u+x test.py

and try to run it

sudo ./test.py

a similar error occurs

Syntax error: "(" unexpected

Then I decided to inject the code directly into the python shell using

sudo python

>>>for each in range(5):
...    print 'hello'

:

hello
hello
hello
hello
hello

, python , .py

, , , , , .

!

+4
2

: python filename.py, #!/usr/bin/python Python.

: Linux ( , Raspberry Pi), , script ( Bash script). Bash ( - , Bash), Bash Python. ( Python), " " , #! ( "-", "shebang" ). #!, , . /usr/bin/python Python. ( /usr/bin/env python , /usr/bin/python, Python , , , .)

, #!/usr/bin/python Python, Linux, , All Just Work ™.

, STOP, sudo ! .

+7

,

#!/usr/bin/env python

bash ( ), Python.

, script :

$ python test.py

, script.

, sudo, .

+3

Source: https://habr.com/ru/post/1611246/


All Articles