djh*_*987 2 python syntax-error
当我将这个python代码放入REPL for python(交互式shell)时,它按预期工作:
>>> def get_header():
... return (None,None,None)
...
>>> get_header()
(None, None, None)
Run Code Online (Sandbox Code Playgroud)
请注意,return语句缩进了四个空格,我已经检查以确保没有多余的空格.
当我将完全相同的代码放入python脚本文件并执行它时,我收到以下错误:
./test.py: line 1: syntax error near unexpected token `('
./test.py: line 1: `def get_header():'
Run Code Online (Sandbox Code Playgroud)
为什么?
编辑:这是test.py,空格和所有内容的确切内容:
def get_header():
return (None,None,None)
get_header()
Run Code Online (Sandbox Code Playgroud)
我已经验证上面的脚本(test.py)确实产生了上面的错误,如上所述.
Fat*_*man 11
这不起作用的原因是你没有任何东西告诉bash它这是一个Python脚本,所以它试图将它作为shell脚本执行,然后在语法不正确时抛出错误.
你需要的是用一个shebang行启动文件,告诉它应该运行什么.所以你的文件变成:
#!/usr/bin/env python
def get_header():
return (None, None, None)
print get_header()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
422 次 |
| 最近记录: |