Failed to "import matplotlib.pyplot as plt" in virtualenv

I work with a flask in a virtual environment. I was able to install matplotlib with pip, and I can import matplotlib in a Python session. However, when I import it as

 matplotlib.pyplot as plt 

I get the following error:

 >>> import matplotlib.pyplot as plt Traceback (most recent call last): File "<stdin>", line 1, in <module> File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 109, in <module> _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup globals(),locals(),[backend_name],0) File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, 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. 

I am confused by why he asks me to install Python as a framework. Doesn't it already exist? What does it mean to “install Python as a framework” and how to install it?

+58
python flask matplotlib virtualenv macos
Apr 3 '15 at 14:11
source share
7 answers

This solution worked for me. If you have already installed matplotlib using pip in your virtual environment, you can simply enter the following:

 $ cd ~/.matplotlib $ nano matplotlibrc 

And then write backend: TkAgg there. If you need more information, just go to the solution link.

+130
Jan 30 '16 at 21:09
source share

I got the same error and tried to answer Jonathan :

You can fix this problem using the Agg backend.

Go to User/yourname/.matplotlib and open / create matplotlibrc and add the following backend : Agg line backend : Agg , and it should work for you.

I run the program, no errors, but also no graphs, and I tried the backend: Qt4Agg , it prints that I do not have PyQt4.

Then I tried another backend: backend: TkAgg , it works!

So maybe we can try the difference, and some may work or install the required packages, such as PyQt4.

Here is an example python snippet that you can try and check matplotlib.

 import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt plt.plot([1, 2, 3], [0, 3, 7]) plt.show() 
+27
Dec 21 '15 at 9:13
source share

I had a similar problem when I used pip to install matplotlib. By default, the latest version is installed, which was 1.5.0. However, I had another virtual environment with Python 3.4 and matplotlib 1.4.3, and this environment worked fine when I imported matplotlib.pyplot. So I installed an earlier version of matplotlib using the following:

 cd path_to_virtual_environment # assume directory is called env3 env3/bin/pip install matplotlib==1.4.3 

I know that this is only work, but it worked for me as a short-term solution.

+11
Oct 31 '15 at 2:39
source share

If you do not want to install the .matplotib/matplotlibrc configuration file, you can work around this problem by setting the 'Agg' backend at run time immediately after importing matplotlib and before importing matplotlib.pyplot :

 In [1]: import matplotlib In [2]: matplotlib.use('Agg') In [3]: import matplotlib.pyplot as plt In [4]: fig, ax = plt.subplots(1, 1) In [5]: import numpy as np In [6]: x = np.linspace(-1., 1.) In [7]: y = np.sin(x) In [8]: ax.plot(x, y) Out[8]: [<matplotlib.lines.Line2D at 0x1057ecf10>] In [9=]: fig.savefig('myplot.png') 

enter image description here

+6
Sep 16 '16 at 20:17
source share

You can fix this problem using the Agg backend Agg

Go to User/yourname/.matplotlib and open / create matplotlibrc and add the following backend : Agg line backend : Agg and it will work for you.

+4
Nov 23 '15 at
source share

Although most of the answers seem to indicate an activate script fix for using system python, I had problems getting this to work, and an easy solution for me - albeit a bit cringey - was to install matplotlib for the global environment and use this instead of the instance virtualenv. You can do this by creating your virtualenv with the -system-site-packages flag, for example virtualenv --system-site-packages foo , or use the universal flag when pip installs, for example pip install -U matplotlib .

0
Nov 10 '15 at 20:09
source share

A clean and simple solution is to create a kernel that sets PYTHONHOME to "VIRTUAL_ENV" and then uses the Python system executable (instead of the virtual one).

If you want to automate the creation of such a kernel, you can use the jupyter-virtualenv-osx script.

0
Jan 30 '17 at 10:47 on
source share



All Articles