You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Macbook M1上Python3.7安装numpy时clang编译错误求助

解决Python 3.7项目中pip-compile安装numpy的clang编译错误

问题场景

我在开发一个依赖Python 3.7的项目,为同步setup.py与requirements.txt的依赖版本,执行了以下命令:

pip-compile --output-file=requirements.txt setup.py

过程中出现numpy编译失败,核心报错:

clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly

完整错误日志:

compile options: '-DNPY_INTERNAL_BUILD=1 -DHAVE_NPY_CONFIG_H=1 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 -DNO_ATLAS_INFO=3 -DHAVE_CBLAS -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/private -Inumpy/core/include -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/include/numpy -Inumpy/core/src/private -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/Users/me/.pyenv/versions/3.7.13/envs/queue-volume-drivers-3.7.13/include -I/Users/me/.pyenv/versions/3.7.13/include/python3.7m -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/private -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/npymath -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/private -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/npymath -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/private -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/npymath -c'
              extra options: '-faltivec -I/System/Library/Frameworks/vecLib.framework/Headers'
              clang: numpy/core/src/multiarray/alloc.c
              clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
              clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
              error: Command "clang -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@3/include -DNPY_INTERNAL_BUILD=1 -DHAVE_NPY_CONFIG_H=1 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 -DNO_ATLAS_INFO=3 -DHAVE_CBLAS -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/private -Inumpy/core/include -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/include/numpy -Inumpy/core/src/private -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/Users/me/.pyenv/versions/3.7.13/envs/queue-volume-drivers-3.7.13/include -I/Users/me/.pyenv/versions/3.7.13/include/python3.7m -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/private -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/npymath -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/private -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/npymath -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/private -Ibuild/src.macosx-12.6-arm64-3.7/numpy/core/src/npymath -c numpy/core/src/multiarray/alloc.c -o build/temp.macosx-12.6-arm64-3.7/numpy/core/src/multiarray/alloc.o -MMD -MF build/temp.macosx-12.6-arm64-3.7/numpy/core/src/multiarray/alloc.o.d -faltivec -I/System/Library/Frameworks/vecLib.framework/Headers" failed with exit status 1
              [end of output]
        
          note: This error originates from a subprocess, and is likely not a problem with pip.
        error: legacy-install-failure
        
        × Encountered error while trying to install package.
        ╰─> numpy

解决方案

核心原因

-faltivec是PowerPC架构专属的编译选项,而当前环境是arm64架构的macOS,clang编译器在arm64下不支持该选项。适配Python3.7的旧版numpy在arm64环境编译时错误启用了该选项,导致编译失败。

解决步骤

  1. 临时屏蔽错误编译选项
    在执行pip-compile前,设置环境变量禁用altivec相关特性:

    export NPY_DISABLE_CPU_FEATURES=altivec
    
  2. 重新执行pip-compile
    保持环境变量生效状态,重新运行命令:

    pip-compile --output-file=requirements.txt setup.py
    
  3. 永久配置(可选)
    若不想每次手动设置,可将环境变量添加到shell配置文件(如~/.zshrc或~/.bashrc):

    echo 'export NPY_DISABLE_CPU_FEATURES=altivec' >> ~/.zshrc
    source ~/.zshrc
    

替代方案:使用预编译包

若上述方法无效,可直接安装适配arm64的numpy预编译包,跳过源码编译:

pip install numpy==1.21.6 --only-binary :all:

注:Python3.7兼容的numpy最新稳定版为1.21.6,确保版本兼容性。

内容的提问来源于stack exchange,提问作者tuemar29

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 09:30:39