How to fix this import matplotlib problem in M1 Mac?

Question:

I bought Mac M1, and now setting up my python environment.

  • I installed Rosetta 2, and set my Terminal as Rosetta 2
  • I installed pyenv with brew
  • installed Sqlite3 with brew
  • installed python 3.9.4 with pyenv (set as global)
  • installed visual studio code with brew
  • installed pandas with pip in Terminal(arm64) (problem solved, thank you hd1)

Now I am trying to import matplotlib after "pip install matplotlib" in Terminal(arm64), it shows this error:

ImportError                               Traceback (most recent call last)
/var/folders/r5/wq0wq8mx0d56rbrbs38jt94w0000gn/T/ipykernel_54247/646680447.py in <module>
      1 import pandas as pd
      2 
----> 3 import matplotlib.pyplot as plt

~/.pyenv/versions/3.9.4/lib/python3.9/site-packages/matplotlib/__init__.py in <module>
    155 
    156 
--> 157 _check_versions()
    158 
    159 

~/.pyenv/versions/3.9.4/lib/python3.9/site-packages/matplotlib/__init__.py in _check_versions()
    149             ("pyparsing", "2.2.1"),
    150     ]:
--> 151         module = importlib.import_module(modname)
    152         if LooseVersion(module.__version__) < minver:
    153             raise ImportError("Matplotlib requires {}>={}; you have {}"

~/.pyenv/versions/3.9.4/lib/python3.9/importlib/__init__.py in import_module(name, package)
    125                 break
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 
    129 

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 architecture

While it says it is wrong architecture, I tried the solution for pandas here. It’s basically pip uninstall it, and then make sure it is installed with my arm64 Terminal. I uninstalled and purged the cache, then "pip installed" with my arm64 Terminal, but still this error.

Any clue for that? Again, I am using Visual Studio Code and Jupyter notebook .

Asked By: yts61

||

Answers:

Try these codes, they work for me. As you are using pyenv, you can just pyenv uninstall 3.9.4to clear the instance, and then reinstall pyenv install 3.9.4 again. After that, try the codes below.

python -m pip install cython   
python -m pip install --no-binary :all: --no-use-pep517 numpy
brew install libjpeg
python -m pip install matplotlib

The origin of the codes come from here: https://flutterq.com/pip-install-matplotlib-fails-on-m1-mac/

Answered By: EnzoY

I had exactly that very frustrating error on my brand new MacBook Pro M1.

After a long time struggling I found out that I had to uninstall ALL installed pip packages.

pip3 freeze | xargs pip3 uninstall -y

And then only, I could peacefully reinstall and import all my favourites packages.

Answered By: Bastian Raulier

Try uninstalling, and then reinstalling the package that gives the error. In your case it was ‘kiwisolver’. In my case it was psutil. So the following worked for me:

pip uninstall psutil
pip install psutil

and just replace psutil with ‘whatever package is giving the error’.

Answered By: MattC
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.