如何在 Apple Silicon (ARM / M1) 上安装 SciPy

问题描述 投票:0回答:13

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

python3 -m pip install scipy

我还尝试安装brew中的所有内容,并且

import scipy
可以工作,但是使用它会出现段错误。我已经安装了 ARM 版本的 lapack 和 openblas,但这并不能解决问题。

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

python scipy arm apple-silicon apple-m1
13个回答
76
投票

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

如果

numpy
已安装(从轮子),您需要卸载它:

pip3 uninstall -y numpy pythran

我必须编译

numpy
,这需要
cython
pybind11
:

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

65
投票

这个在浪费时间后对我有用:

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

48
投票

此解决方案适用于我的 M1 机器,

pyenv

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

12
投票

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

conda install numpy scipy matplotlib

8
投票

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

brew install scipy

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


6
投票

我成功在 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}"

3
投票

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


3
投票

这里是 SciPy 维护者。

SciPy 现已在 conda 和 pip 上针对 M1 进行分发。您至少需要 macOS 12.0 (Monterey) 和 Python >= 3.8。我们对 macOS 的支持很好,这意味着您不需要做任何特殊的事情或自行编译,除非您想使用开发版本或更旧的 macOS 版本。

如果您需要编译,请仅使用我们的指南(例如https://scipy.github.io/devdocs/dev/contributor/building.html#building-from-sources)。有大量的移动部件,做其他事情今天可能有用,但明天就不行了。

避免使用 pip 编译 SciPy。按照我链接的指南为您的平台设置系统依赖项后,使用我们的开发人员脚本

python dev.py build

不要使用brew安装Python包,使用环境。 brew 非常适合安装系统依赖项或其他开发工具,但不应用于安装 Python 包。今后,这会带来麻烦。


2
投票

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

########### 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
投票

您要安装哪个版本的 scipy?

对于在 Macbook Air M1 上运行的我,我需要将版本从 scipy==1.5.1 增加到 scipy==1.7.3,所以我想你应该使用 1.7.3 版本或更高版本,一切都会好起来的。 .

 pip install -Iv scipy==1.7.3

或者只需在您的文件requirements.txt中添加这一行:

scipy==1.7.3

2
投票

以下内容对我有用。

我目前正在使用

Python 3.10.8
,使用
brew
安装。

目前,安装

numpy==1.23.4
时,需要
setuptools < 60.0.0

我使用

(brew --prefix)/bin/python3 -m pip
来显式调用由
pip
安装的
python 3.10
中的
brew

这是我刚刚安装的版本。

# python         3.10.8

# pip            22.3
# setuptools     59.8.0
# wheel          0.37.1

# numpy          1.23.4
# scipy          1.9.3
# pandas         1.5.1
# scikit-learn   1.1.3
# seaborn        0.12.1
# statsmodels    0.13.2

# gcc            12.2.0
# openblas       0.3.21
# gfortran       12
# pybind11       2.10.0
# Cython         0.29.32
# pythran        0.12.0

以下是我遵循的步骤:

# setuptools < 60.0.0 is required for numpy==1.23.4 in Python 3.10.8
$(brew --prefix)/bin/python3 -m pip install --upgrade pip==22.3 setuptools==59.8.0 wheel==0.37.1

# uninstall numpy and pythran first
$(brew --prefix)/bin/python3 -m pip uninstall -y numpy pythran

# uninstall scipy
$(brew --prefix)/bin/python3 -m pip uninstall -y scipy

# install prerequisites (with brew)
brew install gcc
brew install openblas
brew install gfortran

# set environment variables for compilers to find openblas
export LDFLAGS="-L/opt/homebrew/opt/openblas/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openblas/include"

# install the prerequisites (with pip)
$(brew --prefix)/bin/python3 -m pip install pybind11
$(brew --prefix)/bin/python3 -m pip install Cython

# install numpy
$(brew --prefix)/bin/python3 -m pip install --no-binary :all: numpy

# install pythran after installing numpy, before installing scipy
$(brew --prefix)/bin/python3 -m pip install pythran

# install scipy
export OPENBLAS="$(brew --prefix)/opt/openblas/lib/"
$(brew --prefix)/bin/python3 -m pip install scipy

# install pandas
$(brew --prefix)/bin/python3 -m pip install pandas

# install scikit-learn
$(brew --prefix)/bin/python3 -m pip install scikit-learn

# install seaborn
$(brew --prefix)/bin/python3 -m pip install seaborn

# install statsmodels
$(brew --prefix)/bin/python3 -m pip install statsmodels

1
投票

根据 这个 Github 问题,Scipy 无法在 MacOS 11 (Big Sur) 上运行。如果这些解决方案都不适合您,我建议您更新操作系统。


0
投票

我用

conda install scipy
来解决这个问题。 Conda 有一个针对 Apple M1 的 scipy 定制版本。如果您不想使用 Conda,请将 macOS 更新到 12。

© www.soinside.com 2019 - 2024. All rights reserved.