无法从腻子运行.py文件,语法错误:单词意外(期望“)”)

问题描述:

我对Python和Linux都是陌生的,因此请尽可能在不提供任何假定知识的情况下进行简单的解释,但是我更愿意投入时间和精力进行学习。

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.

我有一个运行Linux的Raspberry Pi 2(型号B V1.1)。我通过腻子与此pi进行交互。

I have a Raspberry Pi 2 (Model B V1.1) that is running Linux. I interact with this pi via putty.

我正在尝试创建一个简单的竞争性反射游戏,该游戏由2个按钮和一个LED组成。我的目标是使LED在短暂的间隔后点亮,并且第一个按下其按钮的玩家获胜。

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.

我正在使用python编写脚本(具体是2.7.3)

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

我的问题是我无法从腻子中运行任何.py文件,我总是收到相同的错误:

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 ")")

要确定问题是否是我的代码中的错误,我创建了一个非常简单的.py文件,以检查是否发生了相同的错误,并且确实如此。因此,我目前认为,即使我的代码可以正常工作,某些原因也会阻止我运行任何.py文件。

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:

首先,我从腻子内部创建一个新的python文件:

First I create a new python file from within putty:

sudo nano test.py

下一步,我输入我的python代码(目前非常简单,因为我无法获取任何.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'

然后按CTRL + O写入文件,按Enter,然后按CTRL + X退出

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

最后,我使用

sudo chmod u+x test.py

并尝试运行它

sudo ./test.py

再次发生类似的错误

Syntax error: "(" unexpected

然后我决定直接将代码输入python shell ,对于范围(5)中的每一个,使用

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

因此直接从外壳执行python代码没有问题,我只是无法执行任何以前保存的.py文件

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.

预先感谢!

简短答案:可以将它们作为 python运行filename.py ,或者将#!/ usr / bin / python 行添加到Python脚本的顶部。

Short answer: Either run these as python filename.py, or else add the line #!/usr/bin/python to the top of your Python scripts.

长答案:当您从Linux中的命令行运行文件时(Raspberry Pi正在运行),默认情况下它将假定该文件是一个shell脚本文件(通常是Bash脚本)。因此,它使用Bash Shell(或其他外壳,但通常是Bash)来解释文件,而Bash不知道Python语法。如果要使用其他解释器(在本例中为Python)运行文件,则必须在文件顶部以#!开头添加魔术线。 >(通常发音为 hash-bang,有时也简称为 shebang)。跟随#!字符的是解释器要使用的完整路径,例如 / usr / bin / python 用于Python脚本。 (您也可以使用 / usr / bin / env python 作为另一个建议的答案;我更喜欢 / usr / bin / python ,因为不可能以这种方式获取错误的Python解释器。但这正在进入高级主题,而这些主题可能现在已经超出您的需求。)

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.)

因此,当您将在Python脚本顶部的#!/ usr / bin / python 行中,您告诉Linux系统使用哪个解释器运行程序,然后它应该全部

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™.

此外,停止使用sudo编辑并运行它们!那只是自找麻烦。

Also, STOP using sudo to edit and run these! That's just asking for trouble.