【问题标题】:Python3 porting issuePython3 移植问题
【发布时间】:2018-04-28 20:41:29
【问题描述】:

在 linux 环境下运行名为shuf-new.py 的文件时,当我使用 python 2 时,以下命令有效:

./shuf.py -e bob

但是,当我将代码的第一行更改为 #!/usr/bin/python3 时,出现以下错误:

-bash: ./shuf-new.py: /usr/bin/python3: bad interpreter: No such file or directory

我不知道如何解决这个问题。

【问题讨论】:

  • 你确定你已经安装了python3?尝试运行which python3,如果找到它,请将/usr/bin/python3 替换为那个或/usr/bin/env python3
  • #!/usr/bin/env python3 是 Unix 系统上推荐的标准 shebang。如果您想强制指定一个特定的 Python 解释器,而不是通过键入 python3 在命令行中获得的同一个解释器,那么您应该跳过 env 并使用该解释器的路径。但是你不想在这里,因为你甚至不知道任何特定解释器的路径是什么(并且不需要知道或关心)。
  • 请避免多次询问同一个问题。 How to convert turn a python file into a shell script 的可能重复项

标签: python linux bash python-3.x shell


【解决方案1】:

虽然添加 shebang #!/usr/bin/python3一种使用 Python 3 执行代码的方式,但它并不是唯一的方式,也不能保证它一定会起作用,因为Python 3 可能安装在/usr/bin 以外的目录中。

如果您的机器上安装了 Python 3(通过尝试在 shell 中运行 python3 来仔细检查),您可以始终使用 python3 your_file.py 运行 Python 3 代码。

如果您想使用 shebang 方法,请使用 #!/usr/bin/env python3 或使用 which python3 找到 python3 可执行文件的确切位置。

【讨论】:

  • 更重要的是,一些发行版已经将 python3 称为 python 的“主要的,go-to-guy”版本,所以当你调用 $ python foo.py 它会调用 python3,如果你想要 python2 ,你需要发出$ python2 foo.py(这是并且已经有一段时间了,例如Arch)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
相关资源
最近更新 更多