如何在M1 Pro版MacOS 13上安装grpcio v1.47.0?
在M1 Pro MacOS 13.0上安装grpcio 1.47.0的解决方案
问题描述
在搭载M1 Pro的MacOS v13.0系统上,使用pip和Python v3.8.13安装grpcio v1.47.0时失败,核心报错为std::isnan和std::signbit的宏冲突:
clang -w -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I /opt/homebrew/opt/openssl/include -D_WIN32_WINNT=1536 -DGRPC_XDS_USER_AGENT_NAME_SUFFIX=\"Python\" -DGRPC_XDS_USER_AGENT_VERSION_SUFFIX=\"1.47.0\" -DOPENSSL_NO_ASM=1 -DGPR_BACKWARDS_COMPATIBILITY_MODE=1 -DHAVE_CONFIG_H=1 -DGRPC_ENABLE_FORK_SUPPORT=1 \"-DPyMODINIT_FUNC=extern \"C\" __attribute__((visibility (\"default\"))) PyObject*\" -DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK=1 -Isrc/python/grpcio -Iinclude -I. -Ithird_party/abseil-cpp -Ithird_party/address_sorting/include -Ithird_party/cares/cares/include -Ithird_party/cares -Ithird_party/cares/cares -Ithird_party/cares/config_darwin -Ithird_party/re2 -I/usr/include/openssl -Ithird_party/upb -Isrc/core/ext/upb-generated -Isrc/core/ext/upbdefs-generated -Ithird_party/xxhash -I/usr/include -I/Users/denis/Library/Caches/pypoetry/virtualenvs/auth-PxTXoSf9-py3.8/include -I/Users/denis/.pyenv/versions/3.8.13/include/python3.8 -c src/core/ext/upb-generated/envoy/config/listener/v3/quic_config.upb.c -o python_build/temp.macosx-13.0-arm64-cpython-38/src/core/ext/upb-generated/envoy/config/listener/v3/quic_config.upb.o -stdlib=libc++ -fvisibility=hidden -fno-wrapv -fno-exceptions -DHAVE_UNISTD_H -pthread In file included from src/core/ext/filters/census/grpc_context.cc:25: In file included from ./src/core/lib/surface/call.h:33: In file included from ./src/core/lib/channel/channel_stack.h:65: In file included from ./src/core/lib/iomgr/call_combiner.h:30: In file included from ./src/core/lib/gprpp/mpscq.h:28: In file included from ./src/core/lib/gprpp/sync.h:25: In file included from third_party/abseil-cpp/absl/synchronization/mutex.h:72: In file included from third_party/abseil-cpp/absl/synchronization/internal/kernel_timeout.h:34: In file included from third_party/abseil-cpp/absl/time/clock.h:26: third_party/abseil-cpp/absl/time/time.h:460:14: error: expected unqualified-id if (std::isnan(n)) ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/math.h:165:5: note: expanded from macro 'isnan' ( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \ ^ In file included from src/core/ext/filters/census/grpc_context.cc:25: In file included from ./src/core/lib/surface/call.h:33: In file included from ./src/core/lib/channel/channel_stack.h:65: In file included from ./src/core/lib/iomgr/call_combiner.h:30: In file included from ./src/core/lib/gprpp/mpscq.h:28: In file included from ./src/core/lib/gprpp/sync.h:25: In file included from third_party/abseil-cpp/absl/synchronization/mutex.h:72: In file included from third_party/abseil-cpp/absl/synchronization/internal/kernel_timeout.h:34: In file included from third_party/abseil-cpp/absl/time/clock.h:26: third_party/abseil-cpp/absl/time/time.h:461:19: error: expected unqualified-id return std::signbit(n) ? -InfiniteDuration() : InfiniteDuration(); ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/math.h:170:5: note: expanded from macro 'signbit' ( sizeof(x) == sizeof(float) ? __inline_signbitf((float)(x)) \ ^
执行的安装命令:
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 CC=\"clang -w\" pip install grpcio==1.47.0
已尝试传入brew安装的absl路径等方案,均未解决。
可行解决方案
1. 使用预编译二进制包(优先推荐)
grpcio v1.47.0提供了arm64架构的预编译包,跳过源码编译即可:
pip install grpcio==1.47.0 --force-reinstall --no-cache-dir --platform macosx_11_0_arm64 --only-binary=:all:
注意:如果使用pyenv管理Python,需确保当前Python版本是针对arm64编译的,避免x86_64兼容模式导致的架构不匹配。
2. 修复源码编译的宏冲突
报错根源是MacOS系统头文件math.h中的isnan/signbit宏与abseil代码中的标准库函数调用冲突,添加编译参数取消宏定义即可:
CFLAGS=\"-I /opt/homebrew/opt/openssl/include -Uisnan -Usignbit\" LDFLAGS=\"-L /opt/homebrew/opt/openssl/lib\" GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1 GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1 CC=\"clang\" pip install grpcio==1.47.0 --no-cache-dir
-Uisnan和-Usignbit:取消系统头文件中的宏定义,让编译器使用std::isnan/std::signbit标准库函数- 移除原命令中的
-w参数,保留编译警告以便排查其他潜在问题
3. 更新Command Line Tools
旧版本的Xcode Command Line Tools可能存在头文件兼容性问题,更新到最新版本后再尝试安装:
xcode-select --install
安装完成后重启终端,再执行安装命令。
内容的提问来源于stack exchange,提问作者Denis Steinman
相关产品推荐
相关产品推荐

