RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more infor

I encountered such a problem when plotting with matplotlib in a virtualenv environment:

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
...

in <module>
from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ

 

It seems to be caused by the different installation configuration of the virtual environment and the default environment.

After searching for the error message, found the solution on STO:

 

1. After installing matplotlib with pip, a .matplotlib directory will be generated in the root directory:

➜ bin ll ~/.matplotlib          

I entered this folder through cd ~/.matplotlib The cd ~ command is to enter the home directory (that is, enter a .matplotlib folder in the home directory)

total 280
-rw-r--r-- 1 me staff 78K 10 4 2015 fontList.cache
-rw-r--r-- 1 me staff 59K 1 17 15:56 fontList.py3k.cache
drwxr-xr-x 2 me staff 68B 10 4 2015 tex.cache

 

2. Create a file named matplotlibrc in this directory with the following contents:

backend: TkAgg

Then save and exit, restart the Python interactive interface or rerun the script, and import is executed normally.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337817&siteId=291194637