我在 Solaris 环境中运行 Python 脚本时遇到以下问题。

看来我在 shebang 行上做了一些不正确的事情,但我不知道这是 Python 3 问题还是命令行问题。

但我怀疑它以某种方式与 shebang 行有关,因为当我在命令行上显式运行 Python 解释器时没有问题。

路径 /opt/python3.3.2/bin/python3.3 是我的系统管理员选择放置 Python 的位置,我不知道这个位置在 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

编辑: 我可以确认 test.py 中的行结尾是 Unix

编辑 2: od 输出
$ 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

编辑 3: shell 是 bash
$ echo $0
/bin/bash

最佳答案

一切重要的事情都发生在评论中。让我简单总结一下。

在彻底检查 shebang 行本身是否正确编写后,考虑到了来自其他系统的类似错误 - 我知道 - 被考虑在内。

事实证明,Solaris 受到与讨论和解决的 here 相同的问题的影响。总结:shebang 行要求解释器是一个二进制文件,而不是另一个脚本。

关于Python3 shebang 线没有按预期工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45444823/

10-12 18:34