Forums

ImportError: No module named cairo

I am trying to import cairo in a python script but I get the following error: "ImportError: No module named cairo". A have a script that performs some drawing into a PDF file, but I am using a simple script as a test. It draws a 100x100 rectangle at position 10x10:

import math,cairo

width, height = 950,580
surface = cairo.PDFSurface ("draw.pdf", width, height)
global ctx
ctx = cairo.Context (surface)
ctx.set_source_rgb(1,1,1)
ctx.rectangle(0,0,width,height)
ctx.fill()

ctx.set_line_width(0.1)
ctx.set_source_rgb(0, 0, 0)
ctx.rectangle(10,10,100,100) 
ctx.stroke()

I have seen some posts concerning this import error message but these posts are using matplotlib.pyplot. It doesn't use cairo directly. Can anyone give me a tip of how to import cairo?

We chose cairocffi as the cairo interface that we pre-install. It's a drop-in replacement for pycairo so you should be able to use it just by changing your import.

Thanks, the tip has worked!