matplotlib: RuntimeError: Python is not installed as a framework
PythonMacosPython 2.7MatplotlibPython Problem Overview
This question has been asked before, in here, also here. However, the solution didn't fix the problem for my case.
The original error is, when I try to import matplotlib.pyplot
, I got:
> Traceback (most recent call last): File "
I followed the solutions to add a ~/.matplotlib/matplotlibrc
file with the code: backend: TkAgg
. After doing that, my error changed to:
> /Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273: > UserWarning: Matplotlib is building the font cache using fc-list. This > may take a moment. warnings.warn('Matplotlib is building the font > cache using fc-list. This may take a moment.') objc[25120]: Class > TKApplication is implemented in both > /Users/XX/anaconda/lib/libtk8.5.dylib and > /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the > two will be used. Which one is undefined. objc[25120]: Class TKMenu is > implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and > /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the > two will be used. Which one is undefined. objc[25120]: Class > TKContentView is implemented in both > /Users/XX/anaconda/lib/libtk8.5.dylib and > /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the > two will be used. Which one is undefined. objc[25120]: Class TKWindow > is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and > /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the > two will be used. Which one is undefined.
I have no idea how to fix that. I'm not using a virtual machine. Could you help me? Thank you!
PS: I found out that by adding:
> import matplotlib
> matplotlib.use('TkAgg')
before import matplotlib.pyplot
, it seems to work. But adding those two lines of codes every time is annoying... Does anyone know what's going on and how I can fix it? Thank you!
Python Solutions
Solution 1 - Python
I run my script in virtualenv. Python version is 3.5.
Add a line:
backend: TkAgg
in file:
~/.matplotlib/matplotlibrc
This solved the problem.
If you want to know more about why adding this solves the problem, you can read about customizing matplotlib's backend. And TkAgg solves this issue because of it's dependency with Tkinter.
Solution 2 - Python
Below worked for me:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Reference: https://github.com/tensorflow/tensorflow/issues/2375
Solution 3 - Python
I installed Jupyter Notebook in virtualenv and below worked for me:
At Console:
echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
pip install matplotlib==2.1.0
On Notebook:
import matplotlib
Restart the kernel:
dataframe.plot.hist()
...
In my opinion, it will fix the error.
Solution 4 - Python
I had the same issues in python 2.7 in virtualenv and I managed to fix those by simply downgrading matplotlib to version 2.1.0
Solution 5 - Python
I was also having the same error. What I have done is to install miniconda packages and using the pythonw. Python app was already installed in my mac. Installation is as simple as breaking an egg. Just bash the .sh file in the terminal.
Solution 6 - Python
I was using pyenv
so the matplotlibrc
path wasn't in the home directory, so I created this script to figure the path out and change the backend
to Tkagg
:
vim $(python -c "import os,matplotlib; print(os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/matplotlibrc'));")
This script should work with any python on your system though.