line 1: syntax error near unexpected token
Permission denied
以test.py为例,脚本内容如下:
def test():
print 'hello, world'
if __name__ == "__main__":
test()
运行脚本:
python test.py
输出:
hello, world
换一种方法运行:
./test.py
会提示出错,文件无可执行权限:
-bash: ./test.py: Permission denied
将文件设为可执行:
chmod a+x test.py
继续运行:
./test.py
提示:
./test.py: line 1: syntax error near unexpected token (' ./test.py: line 1:
def test():'
那是因为系统默认该脚本是shell脚本,把它当shell语句执行,当然失败了。
在前面加上
#!/usr/bin/python
申明l这是个python脚本,要用python解释器来运行:
./test.py
输出:
hello, world
这个东东常用在cgi脚本中,apache启动cgi脚本时就靠它来知道这是个python脚本,执行它需要的python解释器路径在哪里。
有时候写 #!/usr/bin/python 还是不行,很简单,因为python解释器没有装在/usr/bin/目录,改成其所在目录就行了,或者更通用的方法是:
#!/usr/bin/env python
参考链接:
https://blog.csdn.net/weixin_43803904/article/details/102654135
网友评论