Unable to install Matplotlib with Python 3 on M1 Mac using pip3
Asked Answered
E

6

8

I'm unable to install Matplotlib through pip on my M1 Mac. I have Python 3.9.1 installed through Homebrew. I've tried the solution here: Pip install matplotlib fails on M1 Mac but it does not work for me.

I get this long error:

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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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.

I am able to import Matplotlib with Python 2.7 on my Rosetta Terminal but I am trying to use Matplotlib with Python 3 instead.

Eisenberg answered 15/2, 2021 at 8:20 Comment(1)
Does this answer your question? Pip install matplotlib fails on M1 MacReseau
S
12

November 2021 Update

Just run the following command:

$ pip3 install matplotlib

Worked on Mac mini (M1, 2020), Monterey 12.0.1. Python 3.9.7

Feb 2021 Answer Below

In order to install matplotlib with pip3 on an M1 mac, you first need to install the dependencies Cython, numpy and Pillow from source. The following method worked for me.

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

libjpeg is necessary for Pillow so you might want to install it via brew.

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

Then you need to manually download qhull and extract the archive. (http://www.qhull.org/download/qhull-2020-src-8.0.2.tgz on Qhull Downloads) You will get a folder named qhull-2020.2. You then have to place the folder at matplotlib/build. The build folder may not exist so you might have to create it.

Lastly, the following command will install the matplotlib on your M1 mac.

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

Tested for Python 3.9.1 on non-Rosetta terminal on M1 Mac mini (Big Sur 11.2.1).

Stinger answered 18/2, 2021 at 6:15 Comment(4)
I successfully installed matplotlib by following your instructions. Thank you . But I have this error: ImportError: dlopen(/Users/yeung/.pyenv/versions/3.9.4/lib/python3.9/site-packages/kiwisolver.cpython-39-darwin.so, 2): no suitable image found. Did find: /Users/yeung/.pyenv/versions/3.9.4/lib/python3.9/site-packages/kiwisolver.cpython-39-darwin.so: mach-o, but wrong architecture /Users/yeung/.pyenv/versions/3.9.4/lib/python3.9/site-packages/kiwisolver.cpython-39-darwin.so: mach-o, but wrong architectureSopping
it says "Successfully installed matplotlib-3.5.0.dev1355+gb9325758d7"Sopping
When I run 'brew install libjpeg' in non-rosetta terminal, it tells me to use brew under /opt/homebrew. So I just run '/opt/homebrew/bin/brew install libjpeg' and it actually succeeds with version 9 of jpeg library. But then the installation of Pillow still errors out and prompts many error info which looks like related with jpeg. Any suggestion?Amphicoelous
Thank you very much, unfortunately today it gives a: ERROR: Disabling PEP 517 processing is invalid: project specifies a build backend of setuptools.build_meta in pyproject.tomlDismount
E
2

I solved this by just uninstalling homebrew first and downloaded matplotlib with python3. After that i reinstalled homebrew using non-rosetta terminal. Idk how it works cause im very new to these stuff but what works works i guess.

Eisenberg answered 17/2, 2021 at 3:11 Comment(0)
C
2

I solved this by realizing that I had two seperate Python instances installed. When you run which python3 you get the path of your Python installation. Check to see if the path corresponds to the Python version you installed.

I fixed it by uninstalling the version of Python3 I had installed with homebrew with brew uninstall python3.

When I did which python3 again it showed a different Python installation path. I then did pip3 install Matplotlib and I was able to import Matplotlib.

Cavy answered 25/5, 2021 at 15:42 Comment(0)
F
0

1. Uninstall Following Package

pip uninstall pillow
pip uninstall numpy
pip uninstall wordcloud
pip uninstall matplotlib 
pip uninstall pyparsing
pip uninstall kiwisolver
pip uninstall python-dateutil
pip uninstall cycler
pip uninstall packaging
pip uninstall fonttools
pip uninstall contourpy
pip uninstall six

2. Install matplotlib

pip install matplotlib
Forewing answered 25/1, 2023 at 7:16 Comment(0)
C
-1

I originally installed python 3.9 using homebrew, but my matplotlib problem was fixed by uninstalling python and reinstalling using the download from the python website(because of architecture problems).

Chaos answered 20/7, 2021 at 2:26 Comment(0)
C
-2

I found another nice way with pyenv,miniforge and conda.

  • 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

Maybe it is not perfect, but it works for a variety of moduls and you can even choose python=3.8 and other python versions and everything is running on the M1.

Clea answered 3/5, 2021 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.