Search code examples
pythonnumpypython-poetry

Numpy installation fails when installing with Poetry on M1 and macOS


I have a Numpy as a dependency in Poetry pyproject.toml file and it fails to install.

  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
  • macOS Big Sur
  • Python 3.9 installed through Homebrew

How to solve it?

If I install Numpy with pip it installs fine.


Solution

  • Make sure you have OpenBLAS installed from Homebrew:

    brew install openblas
    

    Then before running any installation script, make sure you tell your shell environment to use Homebrew OpenBLAS installation

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

    If you get an error

                    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
    

    This should have been fixed in the recent enough Python packaging tools.

    Make sure

    • Poetry is recent enough version
    • Numpy is recent enough version
    • Any dependency using Numpy, like Scipy or Pyarrrow are also the most recent version

    For example in your pyproject.toml

    [tool.poetry.dependencies]
    # For Scipy compatibility
    python = ">=3.9,<3.11"
    
    scipy = "^1.8.0"
    pyarrow = "^7.0.0"
    

    Even if this still fails you can try to preinstall scipy with pip before running poetry install in Poetry virtualenv (enter with poetry shell) This should pick up the precompiled scipy wheel. When the precompiled wheel is present, Poetry should not try to install it again and then fail it the build step.

    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
    

    After this run Poetry normally:

    poetry install