简体   繁体   English

Bazel 构建失败并显示 msg “env: python: No such file or directory” (macOS Monterey)

[英]Bazel build fails with msg "env: python: No such file or directory" (macOS Monterey)

I am new to python world and I have followed a few articles to set python on my system.我是 python 世界的新手,我已经按照一些文章在我的系统上设置 python。 I need python to build my project through bazel.我需要 python 通过 bazel 构建我的项目。

When I build my project on local , I get the following error.当我在local上构建项目时,出现以下错误。 Please note, I am able to build the project successfully on server so issue is not related to code.请注意,我能够在服务器上成功构建项目,因此问题与代码无关。

env: python: No such file or directory
INFO: Elapsed time: 1.708s, Critical Path: 1.09s
INFO: 7 processes: 4 darwin-sandbox, 3 worker.
FAILED: Build did NOT complete successfully

Please HELP ME to solve this issue with my build failure.帮助我解决我的构建失败的问题。

I tried to solve this by applying the solutions online but all in vain.我试图通过在线应用解决方案来解决这个问题,但都是徒劳的。 I have removed my complete python env and have re-configured it as well but that did not work for me either.我已经删除了我的完整 python 环境并重新配置了它,但这对我也不起作用。

This is my system configuration:这是我的系统配置:

Python version in env is: Python 3.9.12 but it still says "env: python: No such file or directory" in the above error. env 中的 Python 版本是:Python 3.9.12,但在上述错误中仍然显示“env:python:没有这样的文件或目录”。

└─(13:59:50)──> /usr/bin/env python --version    
Python 3.9.12

If I fire /usr/bin/python3 --version I get Python 3.8.9 as output如果我触发/usr/bin/python3 --version我得到Python 3.8.9作为输出

└─(13:47:35)──> /usr/bin/python3 --version                   
Python 3.8.9

But when cd into the /usr/bin/ and I fire python3 --version command I get Python 3.9.12 as output.但是当cd进入/usr/bin/并触发python3 --version命令时,我得到Python 3.9.12作为输出。

┌─(/usr/bin)──────────────────────────────────────────
└─(13:56:29)──> pwd                                                      
/usr/bin
┌─(/usr/bin)──────────────────────────────────────────
└─(13:56:32)──> python3 --version                                        
Python 3.9.12

Also, both /usr/local/bin/python and /usr/local/bin/python3 point to Python 3.9.12 .此外, /usr/local/bin/python/usr/local/bin/python3都指向Python 3.9.12

┌─(/usr/bin)──────────────────────────────────────────
└─(13:56:41)──> /usr/local/bin/python --version       
Python 3.9.12
┌─(/usr/bin)──────────────────────────────────────────
└─(13:57:46)──> /usr/local/bin/python3 --version      
Python 3.9.12

I am having the same issue.我有同样的问题。 Here is what I found.这是我发现的。 Bazel builds often use py_binary tools, which are launched with a shell script that has the shebang #!/usr/bin/env python (in versions prior to 5). Bazel 构建通常使用py_binary工具,这些工具使用带有 shebang #!/usr/bin/env python的 shell 脚本启动(在 5 之前的版本中)。 But it also launches them inside a subshell that clears the PATH environment variable.但它也会在清除PATH环境变量的子 shell 中启动它们。 (exec env - ...) . (exec env - ...)

This means that there must be a python on the system path.这意味着系统路径上必须有一个python Modifying your PATH won't help nor will using something like pyenv .修改您的PATH将无济于事,也不会使用类似pyenv的东西。 To see which python is being used, run (exec env - python) .要查看正在使用哪个 python,请运行(exec env - python) In macOS 12.3, Apple removed /usr/bin/python .在 macOS 12.3 中,Apple 删除了/usr/bin/python so this fails and prints env: python: No such file or directory .所以这失败并打印env: python: No such file or directory

I haven't found any way to fix this, since Apple also does not let you modify /usr/bin to add a symlink to python3 even as root (due to System Integrity Protection).我还没有找到解决此问题的任何方法,因为 Apple 也不允许您修改/usr/bin以将符号链接添加到python3 ,即使是 root (由于系统完整性保护)。

This is fixed in Bazel 5, but I am not able to upgrade to that since I need to be able to make updates on an older project that has libraries that require Bazel < 4.这在 Bazel 5 中已修复,但我无法升级到该版本,因为我需要能够对具有需要 Bazel < 4 的库的旧项目进行更新。

Note that Github macOS runners are still on macOS 11. That may be why your project still builds on your server.请注意,Github macOS 运行器仍在 macOS 11 上。这可能就是您的项目仍然在您的服务器上构建的原因。

I am blocked at this point.我在这一点上被阻止了。 I think I either need to find a way to add python to the system PATH (working around SIP), or I need to back-port some changes from Bazel 5 to Bazel 3 and use a custom version of Bazel.我想我要么需要找到一种方法将python添加到系统PATH (围绕 SIP 工作),要么我需要将一些更改从 Bazel 5 反向移植到 Bazel 3 并使用自定义版本的 Bazel。 Or I need to do all development and debugging on an older version of macOS.或者我需要在旧版本的 macOS 上进行所有开发和调试。

You are right about your analysis @ Matthew Self .你的分析是对的@Matthew Self The symlink cannot be create due to MacOS's SIP.由于 MacOS 的 SIP,无法创建符号链接。 Even after disabling it you cannot create a symlink in /usr/bin/... since it is read-only.即使禁用它,您也无法在/usr/bin/...中创建符号链接,因为它是只读的。

More info on this here - Cannot create a symlink inside of /usr/bin even as sudo更多信息在这里 - 无法在 /usr/bin 内创建符号链接,即使是 sudo


Now I am also having a similar issue while building a bazel target that is by default using /usr/bin/python for running any python files.现在我在构建一个默认使用/usr/bin/python运行任何 python 文件的 bazel 目标时也遇到了类似的问题。 Since my bazel verison is 3.7.1, so it is giving the similar error as yours after updating the OS Monterery.由于我的 bazel 版本是 3.7.1,因此在更新 OS Monterery 后它会给出与您类似的错误。

But using ln -s /usr/bin/python3 /usr/local/bin/python solved my issue as it created a symlink in /usr/local/bin/... instead of /usr/bin/... and surprisingly bazel picked up this PATH of python by deafault.但是使用ln -s /usr/bin/python3 /usr/local/bin/python解决了我的问题,因为它在/usr/local/bin/...而不是/usr/bin/...中创建了一个符号链接,令人惊讶的是bazel 默认选择了这个 python 的路径。

May be because /usr/local/bin will be listed before /usr/bin , hence it was taking precedence.可能是因为/usr/local/bin将在/usr/bin之前列出,因此它具有优先权。

I just solved this problem.我刚刚解决了这个问题。 The approach I used is to hack bazel 4.1.0.我使用的方法是破解 bazel 4.1.0。

First, check out the bazel 4.1.0 source首先,查看 bazel 4.1.0 源码

By modifying the file: src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt通过修改文件:src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt

change the first line改变第一行

#!/usr/bin/env python #!/usr/bin/env python

into进入

#!/usr/bin/env /Library/Frameworks/Python.framework/Versions/2.7/bin/python #!/usr/bin/env /Library/Frameworks/Python.framework/Versions/2.7/bin/python

You can specify the python2 path here.您可以在此处指定 python2 路径。

Last, you build the modified bazel in command line:最后,您在命令行中构建修改后的 bazel:

$ bazel build //src:bazel $ bazel build //src:bazel

You can get a modified version of bazel.您可以获得修改版的 bazel。 By using this bazel instead of original one, the problem is solved.通过使用这个 bazel 而不是原来的,问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM