首页 > 解决方案 > 无法使用 pip3 在 m1 mac 上使用 python3 安装 matplotlib

问题描述

我无法在 M1 Mac 上通过 pip 安装 matplotlib。我通过 Homebrew 安装了 Python 3.9.1。我在这里尝试了解决方案:Pip install matplotlib 在 M1 Mac 上失败,但它对我不起作用。

我收到了这个很长的错误:

ImportError: dlopen(/opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 2): no suitable image found.  Did find:
    /opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so: mach-o, but wrong architecture
    /opt/homebrew/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so: mach-o, but wrong architecture

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/tmp/pip-install-mrz7uc56/matplotlib_20151701613b4e6fb6955488fa89e81e/setup.py", line 256, in <module>
    setup(  # Finally, pass this all along to distutils to do the heavy lifting.
  File "/opt/homebrew/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
    return distutils.core.setup(**attrs)
  File "/opt/homebrew/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/opt/homebrew/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/opt/homebrew/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/opt/homebrew/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 299, in run
    self.find_sources()
  File "/opt/homebrew/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 306, in find_sources
    mm.run()
  File "/opt/homebrew/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 541, in run
    self.add_defaults()
  File "/opt/homebrew/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 577, in add_defaults
    sdist.add_defaults(self)
  File "/opt/homebrew/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/command/sdist.py", line 228, in add_defaults
    self._add_defaults_ext()
  File "/opt/homebrew/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/command/sdist.py", line 311, in _add_defaults_ext
    build_ext = self.get_finalized_command('build_ext')
  File "/opt/homebrew/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/cmd.py", line 299, in get_finalized_command
    cmd_obj.ensure_finalized()
  File "/opt/homebrew/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/cmd.py", line 107, in ensure_finalized
    self.finalize_options()
  File "/private/tmp/pip-install-mrz7uc56/matplotlib_20151701613b4e6fb6955488fa89e81e/setup.py", line 88, in finalize_options
    self.distribution.ext_modules[:] = [
  File "/private/tmp/pip-install-mrz7uc56/matplotlib_20151701613b4e6fb6955488fa89e81e/setup.py", line 91, in <listcomp>
    for ext in package.get_extensions()
  File "/private/tmp/pip-install-mrz7uc56/matplotlib_20151701613b4e6fb6955488fa89e81e/setupext.py", line 345, in get_extensions
    add_numpy_flags(ext)
  File "/private/tmp/pip-install-mrz7uc56/matplotlib_20151701613b4e6fb6955488fa89e81e/setupext.py", line 468, in add_numpy_flags
    import numpy as np
  File "/opt/homebrew/lib/python3.9/site-packages/numpy/__init__.py", line 145, in <module>
    from . import core
  File "/opt/homebrew/lib/python3.9/site-packages/numpy/core/__init__.py", line 48, in <module>
    raise ImportError(msg)
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.

我可以在我的 Rosetta 终端上使用 python 2.7 导入 matplotlib,但我正在尝试将 matplotlib 与 python3 一起使用。

任何帮助,将不胜感激!

标签: python-3.xmatplotlibpipapple-m1

解决方案


2021 年 11 月更新

只需运行以下命令:

$ pip3 install matplotlib

曾在 Mac mini(M1,2020)、Monterey 12.0.1 上工作。蟒蛇 3.9.7

2021 年 2 月答案如下

为了matplotlib在 M1 mac 上使用 pip3 安装,您首先需要安装依赖项CythonnumpyPillow从源代码安装。以下方法对我有用。

  1. 安装Cython
pip3 install Cython
  1. 安装numpy
$ git clone https://github.com/numpy/numpy.git
$ cd numpy
$ pip3 install . --no-binary :all: --no-use-pep517
  1. 安装Pillow

libjpegPillow 是必需的,因此您可能需要通过 brew 安装它。

$ brew install libjpeg
$ git clone https://github.com/python-pillow/Pillow.git
$ cd Pillow
$ pip3 install . --no-binary :all: --no-use-pep517
  1. 安装matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git

然后您需要手动下载qhull并解压缩存档。(http://www.qhull.org/download/qhull-2020-src-8.0.2.tgzQhull 下载)您将获得一个名为qhull-2020.2. 然后,您必须将文件夹放在matplotlib/build. 该build文件夹可能不存在,因此您可能必须创建它。

最后,以下命令将matplotlib在您的 M1 mac 上安装。

$ cd matplotlib
$ pip3 install . --no-binary :all:

在 M1 Mac mini (Big Sur 11.2.1) 上的非 Rosetta 终端上测试 Python 3.9.1。


推荐阅读