'How to fix this import matplotlib problem in M1 Mac?

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 .



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bastian Raulier