Home » Python » Unable to “import matplotlib.pyplot as plt” in virtualenv

Unable to “import matplotlib.pyplot as plt” in virtualenv

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am working with 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 about why it asks me to install Python as framework. Doesn’t it already exists? What does it mean to “install Python as framework”, and how do I install it?

Answers:

This solution worked for me. If you already installed matplotlib using pip on your virtual environment, you can just type the following:

$ cd ~/.matplotlib
$ nano matplotlibrc

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

Questions:
Answers:

I got the same error, and tried Jonathan‘s answer:

You can fix this issue by using the backend Agg

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

I run the program, no error, but also no plots, and I tried backend: Qt4Agg,
it prints out that I haven’t got PyQt4 installed.

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

So maybe we can try difference backends and some may work or install the requeired packages like PyQt4.

Here is a sample python snippet that you can try and test matplotlib.

import matplotlib

matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [0, 3, 7])
plt.show()

Questions:
Answers:

I had similar problem when I used pip to install matplotlib. By default, it installed the latest version 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. Therefore, I installed the 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 this is only a work-around, but it worked for me as a short-term fix.

Questions:
Answers:

You can fix this issue by using the backend Agg

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

Questions:
Answers:

If you do not want to set a .matplotib/matplotlibrc configuration file, you can circumvent this issue by setting the 'Agg' backend at runtime right 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

Questions:
Answers:

Although most answers seem to point towards patching the activate script to use the system python, I was having trouble getting that to work and an easy solution for me – though a little cringey – was to install matplotlib to the global environment and use that instead of a virtualenv instance. You can do this either by creating your virtualenv with the –system-site-packages flag like virtualenv --system-site-packages foo, or to use the universal flag when pip installing like pip install -U matplotlib.

Questions:
Answers:

A clean and easy solution is to create a kernel that sets PYTHONHOME to ´VIRTUAL_ENV` and then uses the system Python executable (instead of the one in the virtualenv).

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