Bazel 构建失败,并显示消息“env:python:没有这样的文件或目录”(macOS Monterey)

Abh*_*arg 7 python build python-3.x bazel macos-monterey

我是 python 世界的新手,我按照几篇文章在我的系统上设置 python。我需要 python 通过 bazel 构建我的项目。

\n

当我在local上构建项目时,出现以下错误。请注意,我能够在服务器上成功构建项目,因此问题与代码无关。

\n
env: python: No such file or directory\nINFO: Elapsed time: 1.708s, Critical Path: 1.09s\nINFO: 7 processes: 4 darwin-sandbox, 3 worker.\nFAILED: Build did NOT complete successfully\n
Run Code Online (Sandbox Code Playgroud)\n

帮助我解决构建失败的问题。

\n

我尝试通过在线应用解决方案来解决这个问题,但一切都是徒劳的。\n我已经删除了完整的 python 环境并重新配置了它,但这对我来说也不起作用。

\n

这是我的系统配置:

\n
\n

env 中的 Python 版本是:Python 3.9.12 但在上述错误中仍然显示“env: python: No such file or directory”。

\n
\n
\xe2\x94\x94\xe2\x94\x80(13:59:50)\xe2\x94\x80\xe2\x94\x80> /usr/bin/env python --version    \nPython 3.9.12\n
Run Code Online (Sandbox Code Playgroud)\n
\n

如果我触发/usr/bin/python3 --version我会得到Python 3.8.9作为输出

\n
\n
\xe2\x94\x94\xe2\x94\x80(13:47:35)\xe2\x94\x80\xe2\x94\x80> /usr/bin/python3 --version                   \nPython 3.8.9\n
Run Code Online (Sandbox Code Playgroud)\n

但是当cd进入/usr/bin/and I firepython3 --version命令时,我得到Python 3.9.12作为输出。

\n
\xe2\x94\x8c\xe2\x94\x80(/usr/bin)\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\xe2\x94\x94\xe2\x94\x80(13:56:29)\xe2\x94\x80\xe2\x94\x80> pwd                                                      \n/usr/bin\n\xe2\x94\x8c\xe2\x94\x80(/usr/bin)\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\xe2\x94\x94\xe2\x94\x80(13:56:32)\xe2\x94\x80\xe2\x94\x80> python3 --version                                        \nPython 3.9.12\n
Run Code Online (Sandbox Code Playgroud)\n

另外,/usr/local/bin/python和 都/usr/local/bin/python3指向Python 3.9.12

\n
\xe2\x94\x8c\xe2\x94\x80(/usr/bin)\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\xe2\x94\x94\xe2\x94\x80(13:56:41)\xe2\x94\x80\xe2\x94\x80> /usr/local/bin/python --version       \nPython 3.9.12\n\xe2\x94\x8c\xe2\x94\x80(/usr/bin)\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\xe2\x94\x94\xe2\x94\x80(13:57:46)\xe2\x94\x80\xe2\x94\x80> /usr/local/bin/python3 --version      \nPython 3.9.12\n
Run Code Online (Sandbox Code Playgroud)\n

Mat*_*elf 10

我有同样的问题。这是我发现的。Bazel 构建经常使用py_binary工具,这些工具是通过具有 shebang 的 shell 脚本启动的#!/usr/bin/env python(在 5 之前的版本中)。但它也会在清除PATH环境变量的子 shell 中启动它们。(exec env - ...)

这意味着python系统路径上必须有一个。修改你的PATH不会有帮助,使用类似的东西也不会有帮助pyenv。要查看正在使用哪个 python,请运行(exec env - python). 在 macOS 12.3 中,Apple 删除了/usr/bin/python. 所以这会失败并打印env: python: No such file or directory.

我还没有找到任何方法来解决这个问题,因为苹果也不允许你修改/usr/bin以添加符号链接,python3甚至作为 root (由于系统完整性保护)。

这已在 Bazel 5 中修复,但我无法升级到该版本,因为我需要能够对具有需要 Bazel < 4 的库的旧项目进行更新。

请注意,Github macOS 运行器仍在 macOS 11 上。这可能就是您的项目仍然在服务器上构建的原因。

我此时被阻止了。我想我要么需要找到一种方法来添加python到系统PATH(围绕 SIP 工作),要么需要将一些更改从 Bazel 5 反向移植到 Bazel 3 并使用 Bazel 的自定义版本。或者我需要在旧版本的 macOS 上进行所有开发和调试。