【问题标题】:How to install SciPy on Apple Silicon (ARM / M1)如何在 Apple Silicon (ARM / M1) 上安装 SciPy
【发布时间】:2021-04-21 01:09:51
【问题描述】:

我已经成功地在带有 Apple Silicon 的新 Mac mini 上安装了带有 Numpy 和 Matplotlib 的 python 3.9.1。但是,我无法安装 SciPy:使用时出现编译错误

python3 -m pip install scipy

我还尝试从 brew 安装所有内容,并且“import scipy”可以正常工作,但是使用它会出现 seg 错误。我已经安装了 ARM 版本的 lapack 和 openblas,但这并不能解决问题。

有人成功了吗? (我有兴趣在本地运行它,而不是通过 Rosetta)。

【问题讨论】:

    标签: python scipy arm apple-silicon apple-m1


    【解决方案1】:

    另外,如果有人有这个错误信息>

    ########### CLIB COMPILER OPTIMIZATION ###########
    Platform      :
      Architecture: aarch64
      Compiler    : clang
    
    CPU baseline  :
      Requested   : 'min'
      Enabled     : none
      Flags       : none
      Extra checks: none
    
    CPU dispatch  :
      Requested   : 'max -xop -fma4'
      Enabled     : none
      Generated   : none
    CCompilerOpt.cache_flush[809] : write cache to path 
    

    我在编译 numpy 和 scipy 之前找到了这个解决方案

    原因分析: 从上面的报错信息可以看出最后一个报错说明clang有错误,所以推测应该是编译器导致的错误,因为新版本的xcode命令工具使用的是arm版本的默认编译方式,如果要使用x86架构,需要通过环境变量手动设置具体架构。

    export ARCHFLAGS="-arch x86_64"
    

    示例:

    3c790c45799ec8c598753ebb22/build/temp.macosx-10.14.6-arm64-3.8/ccompiler_opt_cache_clib.py
        ----------------------------------------
    ERROR: Command errored out with exit status 1: /Users/daniel_edu/Projects/PERSONAL/great_expectation_demo/.env/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/zb/c_b9kh2x1px7vl5683rwz8fr0000gn/T/pip-install-y8alaej_/numpy_3d813a3c790c45799ec8c598753ebb22/setup.py'"'"'; __file__='"'"'/private/var/folders/zb/c_b9kh2x1px7vl5683rwz8fr0000gn/T/pip-install-y8alaej_/numpy_3d813a3c790c45799ec8c598753ebb22/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/zb/c_b9kh2x1px7vl5683rwz8fr0000gn/T/pip-record-q9vraevr/install-record.txt --single-version-externally-managed --compile --install-headers /Users/daniel_edu/Projects/PERSONAL/great_expectation_demo/.env/include/site/python3.8/numpy Check the logs for full command output.
    (.env) ➜  great_expectation_demo git:(master) ✗ export ARCHFLAGS="-arch x86_64"
    (.env) ➜  great_expectation_demo git:(master) ✗ pip install --no-binary :all: --no-use-pep517 numpy
    Collecting numpy
      Using cached numpy-1.21.5.zip (10.7 MB)
      Preparing metadata (setup.py) ... done
    Skipping wheel build for numpy, due to binaries being disabled for it.
    Installing collected packages: numpy
        Running setup.py install for numpy ... done
    Successfully installed numpy-1.21.5
    
    

    【讨论】:

      【解决方案2】:

      可以在普通的arm64 brew python上安装,需要自己编译。

      如果numpy 已安装(从轮子),您需要将其卸载:

      pip3 uninstall -y numpy pythran
      

      我必须编译numpy,这需要cythonpybind11

      pip3 install cython pybind11
      

      那么numpy就可以编译了:

      pip3 install --no-binary :all: --no-use-pep517 numpy
      

      Scipy 需要 pythran(这应该在安装 numpy 后发生):

      pip3 install pythran
      

      然后我们需要自己编译scipy,它依赖于fortran和BLAS/LACK:

      brew install openblas gfortran
      

      告诉scipy在哪里可以找到这个库:

      export OPENBLAS=/opt/homebrew/opt/openblas/lib/
      

      然后最后编译scipy:

      pip3 install --no-binary :all: --no-use-pep517 scipy
      

      【讨论】:

      • 在 numpy 之前我必须 brew install openblas gfortran... numpy 至少需要 openblas
      • 不幸的是,SciPy 编译失败,error: Command "ar rcs build/temp.macosx-10.14.6-arm64-3.8/libsuperlu_src.a build/temp.macosx-10.14.6-arm64-3.8/scipy/sparse/linalg/dsolve/SuperLU/SRC/dlaqgs.o [………]" failed with exit status 1 :(
      • 这个答案在 OSX 11.2.3 (20D91) 和 MacBook Pro(13 英寸,M1,2020)上对我有用。我没有像@MattiSG 那样编译失败
      • 我需要 pip install pythran
      • 为什么要使用 --no-use-pep517 强制遗留模式?
      【解决方案3】:

      这个在浪费时间后为我工作了:

      pip install --pre -i https://pypi.anaconda.org/scipy-wheels-nightly/simple scipy
      

      【讨论】:

      • 是的。确实如此。无数个小时之后。这适用于具有 BigSur 版本 11.6 (20G165) 的 MacBook Air(M1,2020)。如果我理解正确,这基本上是来自提供最稳定 SciPy 版本的 anaconda 服务器吗?不确定,它最终如何与 Appl Silicon 配合得很好?非常感谢您的回答。
      • 最佳答案,轻松搞定!
      • @DhruvPatel 这似乎是 Scipy 的“夜间”版本,这意味着它是他们最新的开发版本。显然,他们确实为即将推出的 1.8.0 版本为 ARM 构建(他们没有为当前在 PyPI 上发布的
      • 这种夜间构建方法也适用于我的 Apple M1 上的 scikit-learn,但我需要在命令中添加 --extra-indexpip install --pre --extra-index https://pypi.anaconda.org/scipy-wheels-nightly/simple scikit-learn。来源:scikit-learn.org/stable/developers/advanced_installation.html
      • 不使用anaconda发行版的人怎么办?有其他选择吗?
      【解决方案4】:

      对于那些短期需要它并且不想太忙的人 - 它似乎可以与 python 3.6.4 和 scipy 1.5.4 一起使用(Big Sur 11.5.2,M1 芯片) .

      【讨论】:

      【解决方案5】:

      我设法在 Apple Silicon 上安装了 scipy。我在这里主要遵循 lutzroeder 的指示:https://github.com/scipy/scipy/issues/13409

      这些说明对我来说并不成功,但之后运行“pip3 install scipy”就可以了。我认为这解决了我的问题:

      /opt/homebrew/bin/brew install openblas
      
      export OPENBLAS=$(/opt/homebrew/bin/brew --prefix openblas)
      
      export CFLAGS="-falign-functions=8 ${CFLAGS}"
      

      【讨论】:

        【解决方案6】:

        This solution 使用 pyenv 在我的 M1 机器上工作:

        brew install openblas
        OPENBLAS="$(brew --prefix openblas)" pip install numpy scipy
        

        【讨论】:

        • 这对我有用。我还能够使用相同的方法安装scikit-learnOPENBLAS="$(brew --prefix openblas)" pip install numpy scikit-learn
        【解决方案7】:

        对我来说最简单的解决方案:

        brew install scipy
        

        编辑 PATH 可能是个好主意,因此自制版本将是默认版本。

        【讨论】:

        • 注意,如果你这样做,brew会在这个位置/opt/homebrew/Cellar/python@3.9/3.9.6/bin/python3安装python3.9,并且只有运行这个python可执行文件才能导入scipy
        【解决方案8】:

        您可以从https://github.com/conda-forge/miniforge#miniforge3 安装 miniforge 然后安装这些软件包,

        conda install numpy scipy matplotlib
        

        【讨论】:

        • 我做到了。每当我导入 scipy 的子模块时,这都会产生段错误。不过,Numpy 和 matplotliib 都很好。
        • 你也可以使用 brew 安装 miniforge:brew install --cask miniforge
        • 谢谢!安装对我有用,但是我在虚拟环境中导入已安装的软件包时遇到了困难,我的本地软件包被忽略了。解决方案记录在这里:stackoverflow.com/questions/66768259/…
        猜你喜欢
        • 1970-01-01
        • 2021-05-08
        • 2021-06-20
        • 2021-03-02
        • 2022-01-22
        • 2021-03-11
        • 2022-01-06
        • 1970-01-01
        相关资源
        最近更新 更多