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

Mik*_*maa 13 python numpy python-poetry

我有一个 Numpy 作为 Poetrypyproject.toml文件中的依赖项,但安装失败。

  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
Run Code Online (Sandbox Code Playgroud)
  • macOS 大苏尔
  • 通过 Homebrew 安装 Python 3.9

怎么解决呢?

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

Mik*_*maa 17

确保您已从 Homebrew 安装了 OpenBLAS:

\n
brew install openblas\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
brew install openblas\n
Run Code Online (Sandbox Code Playgroud)\n

如果出现错误

\n
                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\n                  plat_name = get_platform(self.bdist_dir)\n                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\n                  result = calculate_macosx_platform_tag(archive_root, result)\n                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\n                  assert len(base_version) == 2\n              AssertionError\n
Run Code Online (Sandbox Code Playgroud)\n

这个问题应该已经在最近足够多的 Python 打包工具中得到修复。

\n

确保

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

例如在你的pyproject.toml

\n
[tool.poetry.dependencies]\n# For Scipy compatibility\npython = ">=3.9,<3.11"\n\nscipy = "^1.8.0"\npyarrow = "^7.0.0"\n
Run Code Online (Sandbox Code Playgroud)\n

即使这仍然失败,您也可以尝试在 Poetry virtualenv 中运行之前scipy预安装(输入) 这应该会拾取预编译的 scipy 轮子。当预编译轮存在时,Poetry 不应尝试再次安装它,然后导致构建步骤失败。pippoetry installpoetry shell

\n
export OPENBLAS="$(brew --prefix openblas)" \npoetry install\n
Run Code Online (Sandbox Code Playgroud)\n
Collecting scipy\n  Downloading scipy-1.8.0-cp39-cp39-macosx_12_0_arm64.whl (28.7 MB)\n     \xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81 28.7/28.7 MB 6.0 MB/s eta 0:00:00\nRequirement 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)\nInstalling collected packages: scipy\nSuccessfully installed scipy-1.8.0\n
Run Code Online (Sandbox Code Playgroud)\n

之后正常运行 Poetry:

\n
poetry install\n
Run Code Online (Sandbox Code Playgroud)\n