【问题标题】:unable to install matplotlib with python3 on m1 mac using pip3无法使用 pip3 在 m1 mac 上使用 python3 安装 matplotlib
【发布时间】:2021-02-15 08:20:07
【问题描述】:

我无法在我的 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,但我正在尝试将 matplotlib 与 python3 一起使用。

任何帮助将不胜感激!

【问题讨论】:

标签: python-3.x matplotlib pip apple-m1


【解决方案1】:

2021 年 11 月更新

只需运行以下命令:

$ pip3 install matplotlib

曾在 Mac mini(M1,2020)、Monterey 12.0.1 上工作。 Python 3.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.tgzQhull 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 进行了测试。

【讨论】:

  • 我按照您的说明成功安装了 matplotlib。谢谢你 。但我有这个错误: ImportError: dlopen(/Users/yeung/.pyenv/versions/3.9.4/lib/python3.9/site-packages/kiwisolver.cpython-39-darwin.so, 2): no suitable image成立。确实找到了:/Users/yeung/.pyenv/versions/3.9.4/lib/python3.9/site-packages/kiwisolver.cpython-39-darwin.so: mach-o,但是架构错误/Users/yeung/。 pyenv/versions/3.9.4/lib/python3.9/site-packages/kiwisolver.cpython-39-darwin.so:mach-o,但架构错误
  • 它说“成功安装 matplotlib-3.5.0.dev1355+gb9325758d7”
  • 当我在非rosetta终端运行'brew install libjpeg'时,它告诉我在/opt/homebrew下使用brew。所以我只是运行'/opt/homebrew/bin/brew install libjpeg',它实际上在 jpeg 库的第 9 版中成功。但是后来Pillow的安装仍然出错并提示许多看起来与jpeg相关的错误信息。有什么建议吗?
  • 非常感谢,不幸的是今天它给出了一个:错误:禁用PEP 517处理无效:项目在pyproject.toml中指定了setuptools.build_meta的构建后端
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    我通过使用 brew uninstall python3 卸载我用 homebrew 安装的 Python3 版本来修复它。

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

    【讨论】:

      【解决方案4】:

      我发现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上运行。

      【讨论】:

        【解决方案5】:

        我最初使用自制软件安装了 python 3.9,但我的 matplotlib 问题通过卸载 python 并使用从 python 网站下载的重新安装来解决(因为架构问题)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-10
          • 2021-11-01
          • 1970-01-01
          • 2022-11-13
          • 2017-06-20
          • 1970-01-01
          • 2017-10-13
          • 1970-01-01
          相关资源
          最近更新 更多