Python3 shebang line not working as expected

荒凉一梦 提交于 2020-01-14 12:09:40

问题


I'm having the below issue running a Python script in a Solaris environment.

It appears I have done something incorrect on the shebang line, but I can't tell if this is a Python 3 issue or a command line issue.

But I suspect it's related to the shebang line somehow since when I explicitly run the Python interpreter on the command line there is no problem.

The path /opt/python3.3.2/bin/python3.3 is the location where my sysadmin chose to put Python, I don't know of this location is problematic somehow on Solaris.

$ uname -a
SunOS ... 5.10 Generic_150401-49 i86pc i386 i86pc Solaris

$ cat test.py
#!/opt/python3.3.2/bin/python3.3
import sys
print("hi")


$ ./test.py
./test.py: line 2: import: command not found
./test.py: line 3: syntax error near unexpected token `"hi"'
./test.py: line 3: `print("hi")'

$ /opt/python3.3.2/bin/python3.3 test.py
hi

EDIT: I can confirm the line endings in test.py are Unix

EDIT 2: od output

$ od -c -N 30 test.py
0000000   #   !   /   o   p   t   /   p   y   t   h   o   n   3   .   3
0000020   .   2   /   b   i   n   /   p   y   t   h   o   n   3
0000036

EDIT 3: shell is bash

$ echo $0
/bin/bash

回答1:


Everything important happened it the comments. Let me just summarize it.

After thorough checks that the shebang line itself is correctly written, a similar bug from other system - that I was aware of - was taken into consideration.

As it turned out, Solaris is affected by the same issue as was discussed and solved here. Summary: The shebang line requires interpreter to be a binary, not another script.



来源:https://stackoverflow.com/questions/45444823/python3-shebang-line-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!