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

发布于 2025-01-20 02:35:31 字数 1760 浏览 3 评论 0原文

我是Python World的新手,我遵循了一些文章,将Python设置在我的系统上。我需要Python通过Bazel建立我的项目。

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

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

帮助我通过我的构建失败解决这个问题。

我试图通过在线应用解决方案来解决这个问题,但全都徒劳无功。 我已经删除了完整的Python Env,并重新配置了它,但这也对我不起作用。

这是我的系统配置:

env中的python版本是:python 3.9.12,但它仍然显示上述错误中的“ env:python:否这样的文件或目录”。

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

如果我开火/usr/bin/python3 - version 我得到 python 3.8.9 作为输出

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

,但是当cd 中usr/bin/和i Fire 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

另外,/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 new to python world and I have followed a few articles to set python on my system. I need python to build my project through bazel.

When I build my project on local, I get the following error. 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.

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.

└─(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

└─(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.

┌─(/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/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自此以后,行同陌路 2025-01-27 02:35:31

我有同样的问题。这是我发现的。
Bazel构建经常使用py_binary工具,该工具使用带有Shebang #!/usr/bin/bin/env Python的Shell脚本启动(在5个之前的版本中)。但是它也将它们启动在一个子壳中,该子壳清除了路径环境变量。 (exec env- ...)

这意味着系统路径上必须有python。修改您的路径不会有帮助,也不会使用pyenv之类的东西。要查看正在使用哪个Python,请运行(Exec -Env -Python)。在MacOS 12.3中,Apple删除了/usr/bin/python。因此,这将失败并打印env:python:没有这样的文件或目录

我还没有找到任何解决此问题的方法,因为Apple还不能让您修改/usr/bin即使是root(由于系统,因此诚信保护)。

这是在Bazel 5中修复的,但是我无法升级到这一点,因为我需要能够对具有Bazel&lt的库的较旧项目进行更新; 4。

请注意,GitHub MacOS跑步者仍在MacOS 11中。这可能就是为什么您的项目仍在服务器上构建的原因。

此时我被阻止了。我想我要么需要找到一种将python添加到系统path(围绕SIP)添加python的方法,要么我需要回到从Bazel 5到Bazel的一些更改3并使用Bazel的自定义版本。或者,我需要对MacOS的旧版本进行所有开发和调试。

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). But it also launches them inside a subshell that clears the PATH environment variable. (exec env - ...).

This means that there must be a python on the system path. Modifying your PATH won't help nor will using something like pyenv. To see which python is being used, run (exec env - python). In macOS 12.3, Apple removed /usr/bin/python. so this fails and prints 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).

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.

Note that Github macOS runners are still on macOS 11. That may be why your project still builds on your server.

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. Or I need to do all development and debugging on an older version of macOS.

潇烟暮雨 2025-01-27 02:35:31

你的分析是正确的@Matthew Self。由于 MacOS 的 SIP,无法创建符号链接。即使禁用它后,您也无法在 /usr/bin/... 中创建符号链接,因为它是只读的。

有关于此的更多信息 - 无法创建符号链接 现在,我在构建默认情况下使用 /usr/bin/python 运行任何 python 文件的


bazel 目标时也遇到了类似的问题。由于我的 bazel 版本是 3.7.1,因此在更新 OS Monterery 后它会出现与您类似的错误。

但是使用 ln -s /usr/bin/python3 /usr/local/bin/python 解决了我的问题,因为它在 /usr/local/bin/... 中创建了符号链接... code> 而不是 /usr/bin/... ,令人惊讶的是 bazel 默认选择了这个 python 路径。

可能是因为 /usr/local/bin 将列在 /usr/bin 之前,因此它优先。

You are right about your analysis @Matthew Self. The symlink cannot be create due to MacOS's SIP. Even after disabling it you cannot create a symlink in /usr/bin/... since it is read-only.

More info on this here - Cannot create a symlink inside of /usr/bin even as 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. Since my bazel verison is 3.7.1, so it is giving the similar error as yours after updating the 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.

May be because /usr/local/bin will be listed before /usr/bin, hence it was taking precedence.

忆离笙 2025-01-27 02:35:31

我只是解决了这个问题。我使用的方法是黑客bazel 4.1.0。

来查看Bazel 4.1.0源

首先,通过修改文件 :
src/main/java/com/google/google/devtools/build/lib/lib/bazel/pruros/python/python/python_stub_template.txt

更改第一行

#!代码>

进入

#!/usr/bin/env/library/frameworks/python.framework/versions/2.7/bin/python

您可以在此处指定Python2路径。

最后,您在命令行中构建了修改后的Bazel:

$ bazel build // src:bazel

您可以获得Bazel的修改版本。通过使用此Bazel而不是原始的Bazel,解决了问题。

I just solved this problem. The approach I used is to hack bazel 4.1.0.

First, check out the bazel 4.1.0 source

By modifying the file:
src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt

change the first line

#!/usr/bin/env python

into

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

You can specify the python2 path here.

Last, you build the modified bazel in command line:

$ bazel build //src:bazel

You can get a modified version of bazel. By using this bazel instead of original one, the problem is solved.

双马尾 2025-01-27 02:35:31

这可能不是与您的问题完全相同,但是我认为我会分享它,因为我也只需获得初始py_binary即可运行。

我正在运行一个Centos Docker容器,即使已经在我的容器环境中安装了Python3之后也遇到了麻烦。事实证明,我需要在我的路径中安装的Linux软件包,然后由Rules_python内部使用,以找到Python3或Python的确切路径

This is probably not the exact same issue as you were having, but I figured I'd share it since I was also having trouble just getting an initial py_binary to run.

I was running a centos docker container and was having trouble even after having already installed python3 in my container's environment. It turned out, I needed to have the linux package which installed and included in my path, which then gets used internally by rules_python to locate the exact path of python3 or python

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文