无法从 putty 运行 .py 文件,语法错误:词意外(期望“)") [英] Unable to run .py file from putty, Syntax error: word unexpected (expecting ")")

查看:25
本文介绍了无法从 putty 运行 .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)

我的问题是我无法从 putty 中运行任何 .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.

我使用的流程如下:

首先我从 putty 中创建一个新的 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中,使用

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

所以直接从shell执行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.

提前致谢!

推荐答案

Short answer: 要么将这些作为 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 中从命令行运行文件时(树莓派正在运行),默认情况下它假定该文件是一个 shell 脚本文件(通常是 Bash 脚本).所以它使用 Bash shell(或其他一些 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.)

因此,当您将行 #!/usr/bin/python 放在 Python 脚本的顶部时,您是在告诉 Linux 系统使用哪个解释器来运行程序,然后它应该 All Just Work™.

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.

这篇关于无法从 putty 运行 .py 文件,语法错误:词意外(期望“)")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆