在 apple m1 上安装 scipy 和 scikit-learn

Installing scipy and scikit-learn on apple m1

在 m1 芯片上安装以下软件包:Numpy 1.21.1、pandas 1.3.0、torch 1.9.0 和其他一些对我来说效果很好。它们在测试时似乎也能正常工作。但是,当我尝试通过 pip 安装 scipy 或 scikit-learn 时,会出现此错误:

错误:numpy 的构建轮失败

构建numpy失败

错误:无法为使用 PEP 517 且无法直接安装的 numpy 构建轮子

当我已经安装了 pip 的最新版本时,为什么还要重新构建 Numpy?

以前的每个安装都是使用 python3.9 -m pip install ... 在 Mac OS 11.3.1 上使用苹果 m1 芯片完成的。

也许有人知道如何处理这个错误,或者这只是时间问题。

请参阅 scikit-learn 关于

的注释

Installing on Apple Silicon M1 hardware

The recently introduced macos/arm64 platform (sometimes also known as macos/aarch64) requires the open source community to upgrade the build configuation and automation to properly support it.

At the time of writing (January 2021), the only way to get a working installation of scikit-learn on this hardware is to install scikit-learn and its dependencies from the conda-forge distribution, for instance using the miniforge installers:

https://github.com/conda-forge/miniforge

The following issue tracks progress on making it possible to install scikit-learn from PyPI with pip:

https://github.com/scikit-learn/scikit-learn/issues/19137

我不知道为什么要重新构建 numpy(也许 scipy 有其他版本要求?),但我可以报告如何安装 scipy 和 scikit-learn。我刚刚成功使用以下命令序列,基于 Python 3.8.11(特别是 scipy 编译需要一些时间,所以这个过程不是一个快速的过程,不幸的是):

# SciPy:
python -m pip install --no-cache --no-use-pep517 pythran cython pybind11 gast"==0.4.0"
pyenv rehash
python -m pip install --no-cache --no-binary :all: --no-use-pep517 scipy"==1.7.1"

# Scikit-Learn
python -m pip install --no-use-pep517 scikit-learn"==0.24.2"

所以 scikit-learn 主页上关于需要 conda 安装路径的信息似乎已经过时了。

有Apple developer tools (XCode, xcode-select) 和其他一些通过homebrew安装的包,之前在pyenv virtualenv环境中,这也可能是相关,所以我在下面添加它们:

brew install openblas openssl@1.1 pkg-config pyenv pyenv-virtualenv
python -m pip install numpy==1.19.5

我需要较旧的 numpy 版本,因为在此环境中还安装了与较新的 numpy 版本不兼容的 tensorflow;我不知道你的新 numpy 是否有助于解决你遇到的问题。

看来目前对 Apple M1 的支持有很多变化,所以这可能很快就会变得更容易。

最终使用以下步骤对其进行了排序:

  1. 使用 brew 安装 python 3.9 (brew install python@3.9)
  2. 设置 PATH 以首先选择 brew python3 (export PATH=opt/homebrew/bin:$PATH)
  3. 运行 下面的命令安装 scipy 和 scikit-learn


    >> /opt/homebrew/bin/brew install openblas
    >> export OPENBLAS=$(/opt/homebrew/bin/brew --prefix openblas)
    >> export CFLAGS="-falign-functions=8 ${CFLAGS}"
    >> git clone https://github.com/scipy/scipy.git
    >> cd scipy
    >> git submodule update --init
    >> /opt/homebrew/bin/pip3 install .
    >> /opt/homebrew/bin/pip3 install scikit-learn

  1. 进入需要创建虚拟环境的文件夹运行python3 -m venv --system-site-packages .venv

我猜 Scipy 并且 Numpy 在 arm64 处理器(MAC M1 OS BigSur 和更高版本)上不直接支持,这需要用户手动安装和构建依赖项。我通过使用 Python 版本 3.8 创建虚拟环境来解决这个问题。推出最终版本后,可以轻松删除此更改而不会产生任何冲突。

  1. 使用 Python 3.8 版设置 python 虚拟环境。

python3.8 -m venv <environment_name>

  1. 激活环境

source <environment_name>/bin/activate

  1. 安装numpy和scipy

pip3 install numpy
pip3 install scipy

我在这里插话,因为我正在使用 Docker 容器安装所有这些。主机是 M1 MBP 运行ning Monterey 12.0.1 - 我在 GitHub 上的 this thread 中找到了以下解决方案。 scipy 团队似乎很快就会有更新。

确保您使用的是 Python 3.9 并使用 HomeBrew,运行 brew install openblas。完成后,您可以将以下行添加到您的 Docker 文件中:

RUN OPENBLAS="/opt/homebrew/opt/openblas" CFLAGS="-falign-functions=8 ${CFLAGS}" pip3 install scipy
RUN OPENBLAS="/opt/homebrew/opt/openblas" CFLAGS="-falign-functions=8 ${CFLAGS}" pip3 install scikit-learn

最好的方法是使用 Miniforge。已确认 Macbook M1 和 OS 12(蒙特雷)

  1. 在 macbook M1 上,您可以使用命令 brew install miniforge.

    安装 miniforge
  2. 安装完成后,您可以使用创建虚拟环境 命令 conda create -n .venv python

  3. 使用命令conda activate .venv

    激活环境
  4. 安装scikit-learn到环境使用conda install scikit-learn

与 scikit-learn 安装相关的官方问题跟踪器在他们的 github issues 中。一些建议的解决方案适用于 Mac OS 11,但不适用于 Mac OS 12.
我终于找到了可行的解决方案 here

更新:scikit-learn 现在可以通过 pip 运行 ✅

首先 brew install openblas - 它有针对不同处理器的说明 (wikipedia)

brew install openblas
export OPENBLAS=$(/opt/homebrew/bin/brew --prefix openblas)
export CFLAGS="-falign-functions=8 ${CFLAGS}"
# ^ no need to add to .zshrc, just doing this once.
pip install scikit-learn # ==0.24.1 if you want

在 M1 Pro 上表现出色

额外的细节

Pip 从 Pipy 下载源代码,然后针对 MacOS X 12.0 和 arm64 (apple silicon) 构建了 wheel:scikit_learn-1.0.1-cp38-cp38-macosx_12_0_arm64.whl.

Building wheels for collected packages: scikit-learn
  Building wheel for scikit-learn (pyproject.toml) ... done
  Created wheel for scikit-learn: filename=scikit_learn-1.0.1-cp38-cp38-macosx_12_0_arm64.whl size=6364030 sha256=0b0cc9a21af775e0c8077ee71698ff62da05ab62efc914c5c15cd4bf97867b31
Successfully built scikit-learn
Installing collected packages: scipy, scikit-learn
Successfully installed scikit-learn-1.0.1 scipy-1.7.3

关于 Pipy 的注意事项:我们通常下载 预构建的轮子 (是的,这对于可靠分发和确保兼容性非常好)。或者,如果不存在预构建的轮子(悲伤),那么我们下载 a tar.gz 并自己构建它。发生这种情况是因为作者没有向 Pipy 发布预构建的轮子,但越来越多的人将其添加到他们的 CI(github 操作)工作流程中。自己造轮子需要更多 cpu 时间,而且通常不太可靠,但在这种情况下可以。

我们在这里下载一个预制轮子,它几乎没有限制:它适用于 python 3 的任何版本,适用于任何 os,适用于任何架构(如 amd64 或 arm64) : click-8.0.3-py3-none-any.whl

Collecting click>=7.0
  Downloading click-8.0.3-py3-none-any.whl

这里显然我们没有可用的轮子,所以我们必须自己构建它 setuptools 运行 setup.py.

Collecting grpcio>=1.28.1
  Downloading grpcio-1.42.0.tar.gz (21.3 MB)
     |████████████████████████████████| 21.3 MB 12.7 MB/s
  Preparing metadata (setup.py) ... done

## later in the process it installs using setuptools 
Running setup.py install for grpcio ... done

祝你好运,吹笛快乐。

苹果 M1 Pro,macOS 12.1

Python 3.9.10 (main, Jan 15 2022, 11:40:53) 
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
pip 22.0.3

安装 numpy 和 scipy

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

安装 scikit-learn (0.21.3)

pip install cython
brew install libomp
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
export CPPFLAGS="$CPPFLAGS -Xpreprocessor -fopenmp"
export CFLAGS="$CFLAGS -I/opt/homebrew/Cellar/libomp/13.0.1/include"
export CXXFLAGS="$CXXFLAGS -I/opt/homebrew/Cellar/libomp/13.0.1/include"
export LDFLAGS="$LDFLAGS -L/opt/homebrew/Cellar/libomp/13.0.1/lib -lomp"
export DYLD_LIBRARY_PATH=/opt/homebrew/Cellar/libomp/13.0.1/lib
pip install scikit-learn==0.21.3

我通过使用诗歌并在 pyproject.toml 中设置 python 受限依赖项 scipy = { version = "^1.8.0", python = "~3.9" } 解决了这个问题。

截至 2022 年 3 月 14 日(馅饼日!),我所要做的就是在 conda 环境中键入以下内容:

conda install --channel=conda-forge scikit-learn

就是这样。我是 运行 MacBook Pro (13", M1, 2020),运行 MacOS 11.6,在 conda 环境中 python 3.8。