2011-12-28 48 views
2

當我把這個Python代碼到REPL的Python(交互式shell)的罰款,它按預期工作:Python語法錯誤,在REPL

>>> def get_header(): 
...  return (None,None,None) 
... 
>>> get_header() 
(None, None, None) 

注意,return語句由四個縮進空格,我已經檢查過以確保沒有多餘的空格。

當我把確切相同的代碼爲Python腳本文件並執行它,我收到以下錯誤:

./test.py: line 1: syntax error near unexpected token `(' 
./test.py: line 1: `def get_header():' 

爲什麼?

編輯:這是test.py的確切內容,空格和所有:

def get_header(): 
    return (None,None,None) 

get_header() 

我已驗證了上面的腳本(test.py)不會產生上述錯誤,因爲它以上站着。

+1

請發佈test.py,indentation和所有內容。 –

+0

也許在第1-4行? –

+0

我叫shenanigans。至少發佈test.py文件的第1行到第6行。 – tylerl

回答

11

不工作的原因是您沒有任何事情告訴bash這是一個Python腳本,因此它會嘗試將其作爲shell腳本執行,然後在語法不正確時引發錯誤。

你需要的是用shebang行開始文件,告訴它應該運行什麼。所以你的文件變成:

#!/usr/bin/env python 

def get_header(): 
    return (None, None, None) 

print get_header() 
+0

哇。我覺得自己像個傻瓜。謝謝你的一句話:) – djhaskin987