MacBook M1 Sonoma下Python3.7.x编译SciPy1.7.x源码报错求助
安装SciPy 1.7.x(搭配Python3.7)在M1 Sonoma 14.0的编译错误解决
环境概览
已安装的Homebrew包
gcc/13.2.0 openblas/0.3.24 pkg-config/0.29.2_3
Python依赖(pyenv管理的Python3.7.17环境)
Cython/0.29.36 pybind11/2.11.1 pythran/0.14.0 numpy/1.21.6
系统与编译器信息
xcode-select version 2399. Apple clang version 15.0.0 (clang-1500.0.40.1) Target: arm64-apple-darwin23.0.0 MacBook M1, macOS Sonoma 14.0
问题与错误日志
尝试安装SciPy 1.7.3时执行命令:
$OPENBLAS=$(brew --prefix openblas) CFLAGS="-falign-functions=8 ${CFLAGS}" pip3 install --no-binary :all: --no-use-pep517 scipy==1.7.3
出现编译错误,核心日志:
In file included from scipy/spatial/ckdtree.cxx:1007: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/complex:1401:53: error: expected unqualified-id if (__x.real() == 0 && (__x.imag() == 0 || std::isnan(__x.imag()))) ^ scipy/_lib/_c99compat.h:124:30: note: expanded from macro 'isnan' #define isnan(x) ((x) != (x)) ^ 1 warning and 1 error generated. error: Command "clang++ ..." failed with exit status 1
错误原因
SciPy 1.7.x版本的_c99compat.h中定义了isnan宏,而Sonoma系统自带的C++标准库(clang 15.0)在调用std::isnan时,该宏会被展开,破坏了std::isnan的命名空间写法,导致语法冲突。
解决方案
方案1:编译时取消isnan宏定义
修改安装命令,添加-Uisnan参数到CFLAGS和CXXFLAGS,强制取消宏定义避免冲突:
OPENBLAS=$(brew --prefix openblas) \ CFLAGS="-falign-functions=8 -Uisnan ${CFLAGS}" \ CXXFLAGS="-Uisnan ${CXXFLAGS}" \ pip3 install --no-binary :all: --no-use-pep517 scipy==1.7.3
方案2:降级MacOSX.sdk版本
若方案1无效,可尝试适配旧版本SDK:
- 下载MacOSX11.x版本的SDK(从旧版Command Line Tools中提取)
- 将SDK放入路径
/Library/Developer/CommandLineTools/SDKs/ - 指定SDK后执行安装:
export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk OPENBLAS=$(brew --prefix openblas) CFLAGS="-falign-functions=8 ${CFLAGS}" pip3 install --no-binary :all: --no-use-pep517 scipy==1.7.3
依赖版本验证
确认以下依赖版本符合SciPy 1.7.x要求:
- Cython < 3.0(当前0.29.36已满足)
- NumPy 1.16.5 ~ 1.22.x(当前1.21.6已满足)
内容的提问来源于stack exchange,提问作者Balabama Krop
相关产品推荐
相关产品推荐

