Python交互式解释器总是返回“无效语法"在 Windows 上 [英] Python Interactive Interpreter always returns "Invalid syntax" on Windows

查看:38
本文介绍了Python交互式解释器总是返回“无效语法"在 Windows 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常令人困惑的问题.无论我在 Python 解释器中输入什么,都会返回无效语法".请参阅下面的示例.我试过玩弄我运行解释器的提示的代码页,但它似乎根本没有帮助.

I've encountered an extremely confusing problem. Whatever I type into the Python interpreter returns "Invalid Syntax". See examples below. I've tried fooling around with the code page of the prompt I run the interpreter from, but it doesn't seem to help at all.

此外,我还没有在网上其他地方找到这个特殊的、奇怪的错误.

Furthermore, I haven't been able to find this particular, weird bug elsewhere online.

任何人都可以提供的任何帮助都会很可爱.我已经尝试重新安装 Python,但我没有任何运气 - 3.13 和 2.7 中也存在问题.

Any assistance anyone could provide would be lovely. I've already tried reinstalling Python, but I didn't have any luck - the problem is also there in both 3.13 and 2.7.

运行:Python 版本 3.1.3,Windows XP SP3.

Running: Python version 3.1.3, Windows XP SP3.

获取:

C:\Program Files\Python31>.\python
Python 3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.  
>>> 2+2
  File "<stdin>", line 1  
    2+2
       ^     
SyntaxError: invalid syntax

>>> x = "Oh, fiddlesticks."  
  File "<stdin>", line 1  
    x = "Oh, fiddlesticks."  
                           ^  
SyntaxError: invalid syntax

推荐答案

在以交互方式和无缓冲方式运行 Python 时存在一个已知问题,该问题计划在 3.2 中修复 - 并且可能会向后移植到旧版本,请参阅 http://bugs.python.org/issue11098

There is a known problem when running Python interactively and unbuffered, which is scheduled for fixing in 3.2 - and may be backported to older versions, see http://bugs.python.org/issue11098

令人讨厌的是,人们可能在没有意识到的情况下使用了无缓冲的 I/O.就我而言(Python 2.5.4 (r254:67916)),前段时间我设置了一个环境变量,以便 Python 总是 无缓冲地运行(在 Windows 上,这是 PYTHONUNBUFFERED=YES,或者其他什么非空字符串替换为 YES),然后忘记了它.删除 env.var.为我解决了问题.

The nasty thing is, one may be using unbuffered I/O without realising it. In my case (Python 2.5.4 (r254:67916)), I had some time ago set an environment variable such that Python would always run unbuffered (on Windows, this is PYTHONUNBUFFERED=YES, or whatever non-empty string substituted for YES) and then forgot about it. Removing the env.var. solved the problem for me.

所以检查这个环境变量可能是值得的.默认情况下未设置.

So it may be worthwhile checking for this environment variable. It's not set by default.

这篇关于Python交互式解释器总是返回“无效语法"在 Windows 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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