std::runtime_error when loading matplotlib.pyplot
On osx 10.10.5, the examples build cleanly but when I try to run them I get: .libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Error loading module matplotlib.pyplot! /bAbort trap: 6
I suspect this is a system path issue as I have matplotlib installed on the system:
In [9]: matplotlib.path Out[9]: ['/Library/Python/2.7/site-packages/matplotlib']
Hi, I don't know much about mac, but a) Does it work if you try to 'import matplotlib.pyplot' from the python interpreter? b) If yes, does it work if you add PYTHONPATH=/Library/Python/2.7/site-packages to the environment?
I get the same error on Mac.
a) I can import matplotlib.pyplot from python interpreter correctly.
b) after I set PYTHONPATH environment variable, I still get the same error.
I found a solution at https://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python?noredirect=1&lq=1
Create a file ~/.matplotlib/matplotlibrc there and add the following code: backend: TkAgg
I am running into this same issue now, although in a different environment.
I'm running on Ubuntu 18.04. Python version is 3.7. Pip dependencies (i.e. matplotlib) are installed in a virtualenv.
In order for it to be able to import matplotlib, I have to explicitly set my PYTHONPATH like below, even though I have activated the virtualenv:
export PYTHONPATH="/path/to/my/venv/lib/python3.7/site-packages"
With PYTHONPATH set, then I get the error about not being able to import matplotlib.pyplot.
terminate called after throwing an instance of 'std::runtime_error'
what(): Error loading module matplotlib.pyplot!
It's also worth saying that I am building this with bazel, so it's possible that is causing problems. But I don't think it's related to what I'm seeing.
Same error on Windows 10, Visual Studio 2019, Python 310
I had this error in an ubuntu docker 16.04 and 20.04 container. I solved it by first trying to import it as suggested:
python3
import matplotlib.pyplot
From there I got this error:
ModuleNotFoundError: No module named 'Tkinter'
Which once I installed tk everything worked correctly. It is a very misleading error IMO. https://stackoverflow.com/questions/25905540/importerror-no-module-named-tkinter
sudo apt-get install python-tk # ubuntu 16.04
sudo apt-get install python3-tk # ubuntu 20.04
On my system (macos on an M1 Mac, installed python using homebrew), it turned out that I got this error because matplotlib was installed only for Python 3.10, while matplotlib-cpp was using Python 3.11.
Installing matplotlib for that specific Python version helped: python3.11 -m pip install matplotlib
__
p.s. After this, I ran into some other runtime issus. I'm trying to get the examples running.
After undoing what was previously suggested here, I got most (not all!) examples working!
-> Had to remove backend: TkAgg from ~/.matplotlib/matplotlibrc