python idle 2.7.9给出了一个错误的语法错误
我有一个文件:pytest.py,它是一行:python idle 2.7.9给出了一个错误的语法错误,python,python-idle,Python,Python Idle,我有一个文件:pytest.py,它是一行: print "hello world" 空闲-r pytest.py Python 2.7.9 (default, Apr 8 2015, 15:12:41) [GCC 4.2.1 Compatible FreeBSD Clang 3.4.1 (tags/RELEASE_34/dot1-final 208032)] on freebsd10 Type "copyright", "credits" or "license()" for more
print "hello world"
空闲-r pytest.py
Python 2.7.9 (default, Apr 8 2015, 15:12:41)
[GCC 4.2.1 Compatible FreeBSD Clang 3.4.1 (tags/RELEASE_34/dot1-final 208032)] on freebsd10
Type "copyright", "credits" or "license()" for more information.
>>>
*** Error in script or command!
Traceback (most recent call last):
File "pytest.py", line 1
print "hello world"
^
SyntaxError: invalid syntax
>>>
在没有选项的情况下空闲运行,打开并运行文件可以正常工作。这是在FreeBSD 10.0和py27-gtk2-2.24.0_3(python绑定)上实现的。这在某种程度上停止了工作,但我无法将其与具体的更改联系起来。所有软件包/端口都是最新的空闲编译
-r
代码:
但是,默认情况下,编译
:
可选参数flags和dont_继承控制哪些未来语句(参见PEP 236)会影响源代码的编译。如果两者都不存在(或两者都为零),则使用那些在调用compile()的代码中有效的未来语句来编译代码
由于idle的PyShell.py模块,这意味着在-r
中的所有代码都会意外地使用它
将代码更改为打印(“Hello world”)
以解决此问题。作为一个很好的副作用,您的代码也可以在Python3.x中工作
从未来导入打印功能
打印“hello world”
在我的系统上给出相同的结果
Oh poo不能使用vi——答案是正确的当我添加
from __future__ import print_function
到PyShell.py的顶部,作为在中进行错误修复的一部分。我刚刚申请的修正是对第655行的修改
- code = compile(source, filename, "exec")
+ code = compile(source, filename, "exec", dont_inherit=True)
感谢“phihag”指出了问题所在。请发布脚本本身。尝试使用打印功能,看看是否有效。。。这样就排除了Python3实际运行的可能性。请确保在打印之前没有空格。这一定是第一件事。那档案里还有别的东西吗?它为我工作,我有Python 2.7。6@MikeM这将导致缩进错误,所以这不太可能是一个bug。简单测试用例:
py-2-m idlelib.idle-c“print True”
。我打开了https://bugs.python.org/issue24222
并计划添加“dont\u inherit=True”。已应用修复程序。谢谢你的诊断。
- code = compile(source, filename, "exec")
+ code = compile(source, filename, "exec", dont_inherit=True)