2015-05-16 35 views
2

我有一個文件:pytest.py這是一條線:蟒蛇閒置2.7.9提供虛假語法錯誤

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綁定)上。這在某個時候停止了工作,但我無法將其與特定變化聯繫起來。所有的軟件包/端口均達到最新

+0

請發佈腳本本身。 –

+1

嘗試使用print作爲一個函數,看看它是否工作......這樣你排除了Python 3實際運行的可能性。 –

+0

確保'print'之前沒有空格。它必須是線上的第一件事。 –

回答

4

空閒編譯-r代碼as follows

code = compile(source, filename, "exec") 

然而,在默認情況下,compileinherits the calling code's future settings

可選參數標誌和dont_inherit控制,未來的陳述(見PEP 236)會影響信息來源的彙編。如果沒有出現(或兩者都爲零)的代碼與那些效力於正在調用編譯()

由於空閒的PyShell.py模塊does enableprint_function future flags代碼未來報表編制,這意味着無意中您在-r中的所有代碼都必須使用它。

將您的代碼更改爲print("Hello world")即可解決問題。作爲一個很好的副作用,你的代碼也可以在Python 3.x中工作。

+0

這是一個錯誤。簡單的測試用例:'py -2 -m idlelib.idle -c「print True」'。我打開了https://bugs.python。org/issue24222'並計劃添加「dont_inherit = True」。 –

+1

修復應用。感謝您的診斷。 –

0

未來進口print_function

打印的 「Hello World」

給我的系統

哦便便不能用vi在同一個結果 - 答案是正確

3

我在添加

時出現此問題
from __future__ import print_function 

返回到PyShell.py的頂部,作爲回溯Issue 22420中的bug修復的一部分。此修復程序,這是我剛剛在Issue 24222應用,這種變化到線655

-  code = compile(source, filename, "exec") 
+  code = compile(source, filename, "exec", dont_inherit=True) 

感謝「phihag」您指出的問題行。