Failed to load a library: cairo

Viewed 14604

I keep getting this error when trying to run the following command:

python -m weasyprint http://weasyprint.org weasyprint.pdf

The error:

  raise OSError("dlopen() failed to load a library: %s" % ' / '.join(names))
OSError: dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2 / cairo.so.2

I installed Weasyprint using pip install weasyprint. To install cairo i used this doc: https://weasyprint.readthedocs.io/en/latest/install.html

But, at the end of the guide, when i try to run WHERE libcairo-2.dll i get a not found error.

I have been following the guide step by step but it's not working and i keep getting the same error, i searched for various solutions but none of them fixes the issue. Any advice? Thanks in advance

2 Answers

You need to also install the GTK+ libraries (not just weasyprint), in order for weasyprint to render the final PDF.

You can find the installation instructions for your OS documented on weasyprint's website.

Dear Windows user, please follow these steps carefully.

Really carefully. Don’t cheat.

Besides a proper Python installation and a few Python packages, WeasyPrint needs the Pango, cairo and GDK-PixBuf libraries. They are required for the graphical stuff: Text and image rendering. These libraries aren’t Python packages. They are part of GTK+ (formerly known as GIMP Toolkit), and must be installed separately.

If you're running a 64bit version of windows you can grab the GTK installers from here. Once you have that installed... Then running

python -m weasyprint http://weasyprint.org weasyprint.pdf

Should work as expected.

macOS Monterey, I resolved it by just brew install pango and it seems to install all the necessary dependencies including Cairo that I needed. My lucky day!

Related