每次我尝试在.direnv
项目中安装一个将 GRPCIO 作为依赖项的pip
包时,由于我的架构是 arm64,我都会遇到构建失败。在等待GRPCIO工作人员发布更新时,我该如何解决此问题?
Using cached grpcio-1.34.0.tar.gz (21.0 MB)
ERROR: Command errored out with exit status 1:
command: /Users/yoav/Library/Caches/pypoetry/virtualenvs/orca-g6p4a6cZ-py3.8/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/jj/r3j0p91s2qgdpcpf5ptqr9qm0000gn/T/pip-install-n05vw0u0/grpcio_103d947f24954cde8b8a019b4c9f88fe/setup.py'"'"'; __file__='"'"'/private/var/folders/jj/r3j0p91s2qgdpcpf5ptqr9qm0000gn/T/pip-install-n05vw0u0/grpcio_103d947f24954cde8b8a019b4c9f88fe/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/jj/r3j0p91s2qgdpcpf5ptqr9qm0000gn/T/pip-pip-egg-info-z41yqiw6
cwd: /private/var/folders/jj/r3j0p91s2qgdpcpf5ptqr9qm0000gn/T/pip-install-n05vw0u0/grpcio_103d947f24954cde8b8a019b4c9f88fe/
Complete output (10 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/jj/r3j0p91s2qgdpcpf5ptqr9qm0000gn/T/pip-install-n05vw0u0/grpcio_103d947f24954cde8b8a019b4c9f88fe/setup.py", line 359, in <module>
if mac_target and (pkg_resources.parse_version(mac_target) <
File "/Users/yoav/Library/Caches/pypoetry/virtualenvs/orca-g6p4a6cZ-py3.8/lib/python3.8/site-packages/pkg_resources/__init__.py", line 113, in parse_version
return packaging.version.Version(v)
File "/Users/yoav/Library/Caches/pypoetry/virtualenvs/orca-g6p4a6cZ-py3.8/lib/python3.8/site-packages/pkg_resources/_vendor/packaging/version.py", line 275, in __init__
match = self._regex.search(version)
TypeError: expected string or bytes-like object
ASM Builds for BoringSSL currently not supported on: macosx-11-arm64
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
这似乎效果很好,在终端中执行以下操作
export GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1
export GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1
然后安装您要安装的任何软件包,就我而言,我正在尝试安装 firebase 管理员,我必须运行以下内容
pip install firebase-admin
或者,如果您只想安装 grpcio ,只需运行以下命令
pip install grpcio
我发现这个线程有助于详细了解这个问题 https://github.com/grpc/grpc/issues/25082
CFLAGS="-I /opt/homebrew/opt/openssl/include" LDFLAGS="-L /opt/homebrew/opt/openssl/lib" GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1 GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1 pip install -r requirements.txt
这在我的Mac Book Pro M1 Bigsur中对我有用。 感谢您的解决方案
我找到了一个解决方案,可以从 https://github.com/grpc/grpc/issues/25082 更详细地获取并删除某些不必要的步骤。请注意,截至本文发布日期,这仅适用于 python 版本 3.9。
首先,安装pyenv
并pyenv-virtualenv
。
brew install pyenv pyenv-virtualenv
请注意,您可能需要使用 shell 评估变量,因为简单地添加eval "$(pyenv init -)"
似乎不适用于 Apple M1 计算机。相反,请运行以下命令
pyenv init - >> .zshrc
pyenv virtualenv-init - >> .zshrc
然后转到您的 python 项目目录并创建一个.envrc
并将其注册到direnv
echo 'layout pyenv 3.9.1' > .envrc
direnv allow
接下来,通过以下方式安装您的requirements.txt
GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1 GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1 pip install -r requirements.txt
Collecting google-cloud-pubsub
Using cached google_cloud_pubsub-2.3.0-py2.py3-none-any.whl (210 kB)
Collecting proto-plus>=1.7.1
Using cached proto_plus-1.17.0-py3-none-any.whl (42 kB)
Collecting grpc-google-iam-v1<0.13dev,>=0.12.3
Using cached grpc-google-iam-v1-0.12.3.tar.gz (13 kB)
Collecting libcst>=0.3.10
Using cached libcst-0.3.17-py3-none-any.whl (507 kB)
Collecting google-api-core[grpc]<2.0.0dev,>=1.22.2
Using cached google_api_core-1.26.1-py2.py3-none-any.whl (92 kB)
Collecting protobuf>=3.12.0
Using cached protobuf-3.15.6-py2.py3-none-any.whl (173 kB)
Collecting grpcio<2.0.0dev,>=1.0.0
Using cached grpcio-1.36.1.tar.gz (21.5 MB)
Collecting googleapis-common-protos[grpc]<2.0.0dev,>=1.5.2
Using cached googleapis_common_protos-1.53.0-py2.py3-none-any.whl (198 kB)
Collecting typing-inspect>=0.4.0
Using cached typing_inspect-0.6.0-py3-none-any.whl (8.1 kB)
Processing /Users/rkmacmini/Library/Caches/pip/wheels/b7/a5/c4/504d913c2a55bb09c607541578ec5f844d1ff33467abe93ba5/PyYAML-5.4.1-cp39-cp39-macosx_11_0_arm64.whl
Collecting typing-extensions>=3.7.4.2
Using cached typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting six>=1.13.0
Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting google-auth<2.0dev,>=1.21.1
Using cached google_auth-1.27.1-py2.py3-none-any.whl (136 kB)
Collecting pytz
Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Requirement already satisfied: setuptools>=40.3.0 in ./.direnv/python-3.9.1/lib/python3.9/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.22.2->google-cloud-pubsub->-r requirements.txt (line 1)) (49.2.1)
Collecting packaging>=14.3
Using cached packaging-20.9-py2.py3-none-any.whl (40 kB)
Collecting requests<3.0.0dev,>=2.18.0
Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
Collecting mypy-extensions>=0.3.0
Using cached mypy_extensions-0.4.3-py2.py3-none-any.whl (4.5 kB)
Collecting pyasn1-modules>=0.2.1
Using cached pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
Collecting cachetools<5.0,>=2.0.0
Using cached cachetools-4.2.1-py3-none-any.whl (12 kB)
Collecting rsa<5,>=3.1.4; python_version >= "3.6"
Using cached rsa-4.7.2-py3-none-any.whl (34 kB)
Collecting pyparsing>=2.0.2
Using cached pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
Collecting idna<3,>=2.5
Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.4-py2.py3-none-any.whl (153 kB)
|████████████████████████████████| 153 kB 1.1 MB/s
Collecting certifi>=2017.4.17
Using cached certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
Collecting chardet<5,>=3.0.2
Using cached chardet-4.0.0-py2.py3-none-any.whl (178 kB)
Collecting pyasn1<0.5.0,>=0.4.6
Using cached pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
Using legacy 'setup.py install' for grpc-google-iam-v1, since package 'wheel' is not installed.
Using legacy 'setup.py install' for grpcio, since package 'wheel' is not installed.
Installing collected packages: six, protobuf, proto-plus, grpcio, googleapis-common-protos, grpc-google-iam-v1, mypy-extensions, typing-extensions, typing-inspect, pyyaml, libcst, pyasn1, pyasn1-modules, cachetools, rsa, google-auth, pytz, pyparsing, packaging, idna, urllib3, certifi, chardet, requests, google-api-core, google-cloud-pubsub
Running setup.py install for grpcio ... done
Running setup.py install for grpc-google-iam-v1 ... done
Successfully installed cachetools-4.2.1 certifi-2020.12.5 chardet-4.0.0 google-api-core-1.26.1 google-auth-1.27.1 google-cloud-pubsub-2.3.0 googleapis-common-protos-1.53.0 grpc-google-iam-v1-0.12.3 grpcio-1.36.1 idna-2.10 libcst-0.3.17 mypy-extensions-0.4.3 packaging-20.9 proto-plus-1.17.0 protobuf-3.15.6 pyasn1-0.4.8 pyasn1-modules-0.2.8 pyparsing-2.4.7 pytz-2021.1 pyyaml-5.4.1 requests-2.25.1 rsa-4.7.2 six-1.15.0 typing-extensions-3.7.4.3 typing-inspect-0.6.0 urllib3-1.26.4
注意:一些参考资料说你需要将你的CFLAGS等指向brew安装的openssl位置,但这对我来说似乎没有必要。如果您有问题,可以通过以下方式尝试启用CFLAGS的命令
CFLAGS="-I /opt/homebrew/opt/openssl/include" LDFLAGS="-L /opt/homebrew/opt/openssl/lib" GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1 GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1 pip install -r requirements.txt
有一个更简单、更可靠的选择:
pip uninstall grpcio
conda install grpcio
编辑:谷歌现在为苹果硅Mac提供官方的grpcio版本,从1.51.3版本开始。不再需要下载非官方二进制文件。
不幸的是,谷歌不提供为苹果硅Mac制造的grpcio
轮子。正如你从这个问题中看到的,用户已经提出了各种编译器标志来编译 M1 上的库,这些标志并不总是有效。
厌倦了这些问题,我发布了grpcio-mac-arm-build存储库,其中包含一个每日GitHub Actions作业,该作业编译Apple Silicon的最新稳定grpcio
和grpcio-tools
版本。Python 3.8、3.9、3.10 和 3.11 的编译轮可在"发布"页面下载。所有内容都是开源的,并使用与grpcio
库相同的许可证发布。这样,您根本不需要编译库。
经过多次尝试,我终于通过运行以下命令安装了它:
xcode-select --install
brew install openssl re2
export LDFLAGS="-L$(/opt/homebrew/bin/brew --prefix openssl)/lib -L$(/opt/homebrew/bin/brew --prefix re2)/lib"
export CPPFLAGS="-I$(/opt/homebrew/bin/brew --prefix openssl)/include -I$(/opt/homebrew/bin/brew --prefix re2)/include"
GRPC_BUILD_WITH_BORING_SSL_ASM=""
GRPC_PYTHON_BUILD_SYSTEM_RE2=true
GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=true
GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true
pip install grpcio
取自这里: https://candid.technology/error-command-usr-bin-clang-failed-with-exit-code-1/
我设法在不更改操作系统中的任何变量的情况下使其工作。这似乎与python版本问题有关。我使用 3.7.12 失败了。3.8.12 失败。最后,3.10.2 安装了这个软件包。
这些建议都不适合我。有效的方法如下(假设您安装了自制软件):
- 运行
brew install miniconda
(按照说明执行 -conda init "$(basename "${SHELL}")"
。
要么 - 获取你的 ~/.zshrc 要么杀死你的终端并重新打开。
- 运行
conda create --name myenv python=3.10
. - 运行
conda activate myenv
. - 运行
pip install grpcio
.
这在步骤 64 中安装了正确的 arm1 conda env,因此其余的都是可行的。
以下设置对我有用,
用于 https://asdf-vm.com/安装和管理我的python
使用 https://direnv.net 并仅用一行创建$PWD/.envrc
layout python
确保我使用 asdf 安装了最新的 python 3.9
asdf plugin-add python
asdf install python 3.9.15
然后做到了,
direnv allow .
那应该在$PWD/.direnv
下创建一个 python venv ,然后做了
pip install -U pip wheel grpcio
重要提示:3.9.x 以外的 Python 版本对我不起作用,例如 3.10 失败并出现一些clang
问题
您不再需要执行任何操作。现在,社区对Apple Silicon的支持已经赶上了,事情就开箱即用了。祝您编码愉快!