在 M1 和 macOS 上使用 Poetry 安装时 Numpy 安装失败

Numpy installation fails when installing with Poetry on M1 and macOS

我在 Poetry pyproject.toml 文件中有一个 Numpy 作为依赖,但它安装失败。

  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 -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -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-arm64-3.9/numpy/core/src/umath -Ibuild/src.macosx-12-arm64-3.9/numpy/core/src/npymath -Ibuild/src.macosx-12-arm64-3.9/numpy/core/src/common -Inumpy/core/include -Ibuild/src.macosx-12-arm64-3.9/numpy/core/include/numpy -Inumpy/core/src/common -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/Users/moo/Library/Caches/pypoetry/virtualenvs/dex-ohlcv-qY1n4duk-py3.9/include -I/opt/homebrew/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/include/python3.9 -Ibuild/src.macosx-12-arm64-3.9/numpy/core/src/common -Ibuild/src.macosx-12-arm64-3.9/numpy/core/src/npymath -c numpy/core/src/multiarray/array_assign_scalar.c -o build/temp.macosx-12-arm64-3.9/numpy/core/src/multiarray/array_assign_scalar.o -MMD -MF build/temp.macosx-12-arm64-3.9/numpy/core/src/multiarray/array_assign_scalar.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: Failed building wheel for numpy
        Failed to build numpy

如何解决?

如果我用 pip 安装 Numpy,它安装得很好。

确保从 Homebrew 安装了 OpenBLAS:

brew install openblas

然后在 运行 任何安装脚本之前,确保你告诉你的 shell 环境使用 Homebrew OpenBLAS 安装

export OPENBLAS="$(brew --prefix openblas)" 
poetry install

如果出现错误

                File "/private/var/folders/tx/50wn88yd40v2_6_7fvfr98z00000gn/T/pip-build-env-uq7qd2ba/overlay/lib/python3.9/site-packages/wheel/bdist_wheel.py", line 252, in get_tag
                  plat_name = get_platform(self.bdist_dir)
                File "/private/var/folders/tx/50wn88yd40v2_6_7fvfr98z00000gn/T/pip-build-env-uq7qd2ba/overlay/lib/python3.9/site-packages/wheel/bdist_wheel.py", line 48, in get_platform
                  result = calculate_macosx_platform_tag(archive_root, result)
                File "/private/var/folders/tx/50wn88yd40v2_6_7fvfr98z00000gn/T/pip-build-env-uq7qd2ba/overlay/lib/python3.9/site-packages/wheel/macosx_libfile.py", line 356, in calculate_macosx_platform_tag
                  assert len(base_version) == 2
              AssertionError

这应该已经在最近的 Python 打包工具中修复了。

确保

  • 诗是最近的版本
  • Numpy 是足够新的版本
  • 任何使用 Numpy 的依赖项,如 Scipy 或 Pyarrrow 也是最新版本

例如在你的 pyproject.toml

[tool.poetry.dependencies]
# For Scipy compatibility
python = ">=3.9,<3.11"

scipy = "^1.8.0"
pyarrow = "^7.0.0"

即使这仍然失败,您可以尝试在 Poetry virtualenv 中 运行 宁 poetry install 之前用 pip 预安装 scipy (输入 poetry shell)这应该选择预编译的 scipy 轮。当存在预编译轮时,Poetry 不应尝试再次安装它然后在构建步骤中失败。

poetry shell
pip install scipy
Collecting scipy
  Downloading scipy-1.8.0-cp39-cp39-macosx_12_0_arm64.whl (28.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 28.7/28.7 MB 6.0 MB/s eta 0:00:00
Requirement already satisfied: numpy<1.25.0,>=1.17.3 in /Users/moo/Library/Caches/pypoetry/virtualenvs/dex-ohlcv-qY1n4duk-py3.9/lib/python3.9/site-packages (from scipy) (1.22.3)
Installing collected packages: scipy
Successfully installed scipy-1.8.0

此后运行诗歌正常:

poetry install