无法使用 pip3 在 m1 mac 上使用 python3 安装 matplotlib

unable to install matplotlib with python3 on m1 mac using pip3

我无法在我的 M1 Mac 上通过 pip 安装 matplotlib。我通过 Homebrew 安装了 Python 3.9.1。我在这里尝试了解决方案:Pip install matplotlib fails on 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,但我正在尝试使用 python3 的 matplotlib。

如有任何帮助,我们将不胜感激!

我通过先卸载 homebrew 并使用 python3 下载 matplotlib 解决了这个问题。之后我使用非罗塞塔终端重新安装了自制软件。不知道它是如何工作的,因为我对这些东西很陌生,但我猜什么是有效的。

2021 年 11 月更新

只需运行以下命令:

$ pip3 install matplotlib

致力于 Mac mini(M1,2020),Monterey 12.0.1。 Python3.9.7

2021 年 2 月回答如下

为了在 M1 mac 上使用 pip3 安装 matplotlib,您首先需要安装依赖项 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

libjpeg 是 Pillow 所必需的,因此您可能希望通过 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.tgz on Qhull Downloads) 您将得到一个名为 qhull-2020.2 的文件夹。然后您必须将文件夹放在 matplotlib/buildbuild 文件夹可能不存在,因此您可能需要创建它。

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

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

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

我找到了另一个很好的方法 pyenvminiforgeconda

  • pyenv install miniforge3-4.10
  • conda create -n env_new python=3.9
  • conda activate env_new
  • conda install poetry
  • peotry new project
  • poetry add matplotlib

也许它并不完美,但它适用于各种模块,您甚至可以选择 python=3.8 和其他 python 版本,M1 上的一切都是 运行。

我意识到我安装了两个单独的 Python 实例,从而解决了这个问题。当你 运行 which python3 你得到你的 Python 安装路径。检查路径是否对应于您安装的 Python 版本。

我通过卸载 Python3 的版本来修复它,我用 brew uninstall python3.

安装了自制软件

当我再次执行 which python3 时,它显示了不同的 Python 安装路径。然后我做了 pip3 install Matplotlib 并且我能够导入 Matplotlib。

我最初使用自制软件安装了 python 3.9,但我的 matplotlib 问题已通过卸载 python 并使用从 python 网站下载的内容重新安装(由于体系结构问题)得到解决。