在applem1上安装scipy和scikit learn



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

错误:为numpy构建轮子失败

无法构建numpy

错误:无法为使用PEP517的numpy构建轮子,也无法直接安装

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

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

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

更新:scikit-learn现在通过pip工作✅

只是第一个brew install openblas-它有不同处理器的指令(维基百科)

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

在Apple Silicon M1上表现出色🎉

关于Pip如何工作的更多详细信息

Pip从Pipy下载了源代码,然后构建了针对MacOS X 12.0和arm64(苹果硅)的轮子: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的注意事项:我们通常下载预构建的轮子(是的,这对于可靠的分发和确保兼容性非常好)。或者,如果不存在预构建的轮子(sad),那么我们下载一个tar.gz并自己构建它。这是因为作者没有向Pipy发布预构建的轮子,但越来越多的人将其添加到他们的CI(github操作)工作流中。我们自己构建轮子需要更多的cpu时间,而且通常不太可靠,但在这种情况下可以工作。

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

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

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

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

祝你好运,管道快乐。

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

  1. 使用brew安装python 3.9u2028(brew安装python@3.9)
  2. 设置PATH以首先选择brew python3(导出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

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

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

就是这样。我正在conda环境中运行MacBook Pro(13〃,M12020),带有MacOS 11.6和python 3.8。

请参阅scikit-learn关于的注释

在Apple Silicon M1硬件上安装

最近推出的macos/arm64平台(有时也称为macos/aarch64)要求开源社区升级构建配置和自动化,以正确支持它。

在撰写本文时(2021年1月),在该硬件上安装scikit learn的唯一方法是从conda-forge分发版安装scikit learn及其依赖项,例如使用miniorge安装程序:

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

以下问题跟踪了使用pip安装scikit学习PyPI的进度:

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

苹果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

我不知道为什么会再次构建numpy(也许scipy有其他版本要求?),但我可以报告如何安装scipy和scikit学习。我刚刚成功地使用了以下基于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学习主页上关于conda安装路径的信息似乎已经过时了。

有苹果开发人员工具(XCode、XCode select)和其他一些通过自制软件安装的软件包,以前也安装在pyenv虚拟环境中,这可能也很相关,所以我在下面添加它们:

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

我需要旧的numpy版本,因为在这个环境中也安装了tensorflow,它与新的numpy版不兼容;我不知道你的新numpy是否会导致你遇到的问题。

目前看来,在苹果M1支持方面有很多变化,所以这可能很快会变得更容易。

我猜在arm64处理器(MAC M1 OS BigSur及更高版本)上不直接支持Scipy和Numpy,这需要用户手动安装和构建依赖关系。我用Python 3.8版本创建了一个虚拟环境,从而找到了解决方案。一旦最终版本推出,这些更改就可以很容易地删除,而不会产生任何冲突。

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

python3.8-m venv<环境名称>

  1. 激活环境

源<环境名称>仓/激活

  1. 安装numpy和scipy

pip3安装numpy
pip3安装scipy

我在这里插话,因为我使用Docker容器安装所有这些。主机是M1 MBP,运行蒙特雷12.0.1-我在GitHub上的这个线程中找到了以下解决方案。看来scipy团队很快就会有更新。

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

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安装miniorge。

  2. 安装后,您可以使用命令conda create -n .venv python

  3. 使用命令conda activate .venv激活环境

  4. 使用conda install scikit-learn将scikit learn安装到环境中

与scikit学习安装相关的官方问题跟踪器在他们的github问题中。建议的一些解决方案对Mac OS 11有效,但对Mac OS 12无效。
我终于在这里找到了有效的解决方案

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

我在macOS 11.6.8(BigSur)和Apple M1芯片上遇到了同样的问题。

经过大量研究,我发现scipy现在支持苹果M1芯片,但它需要macOS 12。

因此,要在Apple M1芯片上安装scipy,您可以按照以下两种方式进行操作:

  1. 将您的macOS更新到12
  2. 使用Conda管理Python虚拟环境并通过以下方式安装scipy
conda activate myenv
conda install scipy

我希望以上两种方法可以帮助你!

最新更新