Forums

Where has Cairo gone?

Up until a few days ago everything was working fine but since 24th April I get the following error importing matplotlib.

import matplotlib.pyplot as plt
ImportError: No module named cairo

Any idea what changed with matplotlib?

Our recent update did something to it. We'll investigate tomorrow to find out what.

In the meantime, you can put:

matplotlib.use("agg")

before you import pyplot and everything should work.

Thanks Glenn, that has fixed things.

Strange, when I put in the matplotlib.use("agg") line, I get "NameError: name 'matplotlib' is not defined"

Try importing matplotlib

That did it. Thanks.

Hello. very annoying. This won't work for me :

What should I add to this line ?

from pylab import *

Thank you.

Hi there -- Glenn's suggestion above works for me:

Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.use("agg")
>>> from pylab import *
>>>

What happens when you try it?

Hello,

Same error. What has changed since April 25th ?

File "bourse15.py", line 15, in <module>
from pylab import *
File "/usr/local/lib/python2.7/dist-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pylab.py", line 269, in <module>
from matplotlib.pyplot import *
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 98, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/init.py", line 28, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 1, in <module>
import cairo

The rest :

ImportError: No module named cairo

We updated the system image on 26 April, and it looks like Cairo was dropped. We're fixing that, but it's proving difficult. You can work around it with the

import matplotlib
matplotlib.use("agg")

One thing that might not be obvious -- you must do this before trying to use Matplotlib in the session. Once you've used Matplotlib and it has printed the ImportError: No module named cairo error, that particular Python console is broken and needs to be restarted. It does actually print out an error explaining this, but it's easy to miss: it says something like

This call to matplotlib.use() has no effect
because the backend has already been chosen;                             
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,                          
or matplotlib.backends is imported for the first time.