Python:如何解决 Apple Silicon 上的 numpy ImportError?

我通过 miniforge3 在 m1 AppleSilicon 上安装了我的 python 虚拟环境。

执行 conda -create py39 numpy matplotlib pandas python=3.9

我检查了 conda list ,已经安装了 numpy 包。

但是当我执行 import numpy as np 时,ImportError 发生了。

像这样

ImportError                               Traceback (most recent call last)
~/miniforge3/lib/python3.9/site-packages/numpy/core/__init__.py in <module>
     21 try:
---> 22     from . import multiarray
     23 except ImportError as exc:

~/miniforge3/lib/python3.9/site-packages/numpy/core/multiarray.py in <module>
     11 
---> 12 from . import overrides
     13 from . import _multiarray_umath

~/miniforge3/lib/python3.9/site-packages/numpy/core/overrides.py in <module>
      6 
----> 7 from numpy.core._multiarray_umath import (
      8     add_docstring, implement_array_function, _get_implementing_args)

ImportError: dlopen(/Users/frank_qin/miniforge3/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib
  Referenced from: /Users/frank_qin/miniforge3/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so
  Reason: tried: '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/bin/../lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/bin/../lib/libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file)

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
/var/folders/yk/7dwhc7352g3fm0rp04d22qmw0000gn/T/ipykernel_52010/3593848235.py in <module>
----> 1 import numpy as np

~/miniforge3/lib/python3.9/site-packages/numpy/__init__.py in <module>
    148     from . import _distributor_init
    149 
--> 150     from . import core
    151     from .core import *
    152     from . import compat

~/miniforge3/lib/python3.9/site-packages/numpy/core/__init__.py in <module>
     46 """ % (sys.version_info[0], sys.version_info[1], sys.executable,
     47         __version__, exc)
---> 48     raise ImportError(msg)
     49 finally:
     50     for envkey in env_added:

ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "/Users/frank_qin/miniforge3/bin/python"
  * The NumPy version is: "1.21.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Users/frank_qin/miniforge3/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib
  Referenced from: /Users/frank_qin/miniforge3/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so
  Reason: tried: '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/bin/../lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/lib/libcblas.3.dylib' (no such file), '/Users/frank_qin/miniforge3/bin/../lib/libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file)

请告诉我如何解决这个错误!

stack overflow Python: How to solve the numpy ImportError on Apple Silicon?
原文答案

答案:

作者头像

You may refer to this issue: https://github.com/numpy/numpy/issues/12970.

In short, you can try to install openblas by

conda install openblas

This solves my problem.

作者头像

The following sequence of commands worked for me:

conda clean --all
conda install openblas
----
conda uninstall numpy
conda install numpy