Python: OSError: cannot load library libcairo.so.2
Asked Answered
E

8

32

I'm having some trouble running a python script on my Windows 7 platform. I've installed Python and also cairo, last one using "pip". I'm running the script using this command:

C:\Python34>python.exe label/make_label.py

and I get the following error message:

Traceback (most recent call last):
  File "label/make_label.py", line 6, in <module>
    import cairocffi as cairo
  File "C:\Python34\lib\site-packages\cairocffi\__init__.py", line 41, in <modul
e>
    cairo = dlopen(ffi, *CAIRO_NAMES)
  File "C:\Python34\lib\site-packages\cairocffi\__init__.py", line 34, in dlopen

    return ffi.dlopen(names[0])  # pragma: no cover
  File "C:\Python34\lib\site-packages\cffi\api.py", line 118, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "C:\Python34\lib\site-packages\cffi\api.py", line 411, in _make_ffi_libra
ry
    backendlib = _load_backend_lib(backend, libname, flags)
  File "C:\Python34\lib\site-packages\cffi\api.py", line 400, in _load_backend_l
ib
    return backend.load_library(name, flags)
OSError: cannot load library libcairo.so.2: error 0x7e

What I've already done is the following:

  • Added the PATH to GTK/bin in the environmental variable
  • I check the folder GTK/bin and found "libcairo-2.dll" so I renamed it to libcairo.so

I don't know what other information could be useful in solving this but please let me know and I'll try to add it.

Eads answered 29/1, 2015 at 9:44 Comment(2)
You have to make sure Windows (and Python) can find libcairo-2.dll. I doubt Windows/Python is automatically searching in GTK/bin. I'm not sure either that renaming the dll file to an so extension is a good thing to do.Enigmatic
Well, what I did was to add the location of libcairo.dll (and also libcairo.so) to the PATH of environmental variables. I've been assuming that it tries to check that location. How else can I point Python in the right direction?Eads
D
6

It seems cairo depends a shared library which is not in standard search library, however, the python is calling dlopen to dynamic load the library, so you could try to put the libcairo.so.2(if it's a link, then make sure the reference locates at the same folder) in the working directory. You can also try pkg-config to set the environment. see here http://people.freedesktop.org/~dbn/pkg-config-guide.html

Damon answered 29/1, 2015 at 10:0 Comment(2)
You're indeed correct. By putting the libcairo.so file in the working directory, the script could go further. However, it started to ask for more lib-files so I ended up chucking the whole folder there, didn't have to to figure out exactly which ones were needed.Eads
If you follow the link, and install the exe mentioned in the link with all the default options it would work. Note: Make sure you close and reopen the python terminal or jupyter notebook server.Lemmy
O
40

On Mac OS X using homebrew:

brew install cairo
brew install pango
Ontology answered 8/3, 2016 at 23:32 Comment(0)
V
10

I just fixed this on Mac OSX 10.13 with an Anaconda Python installation and cairosvg:

$ conda install cairo pango gdk-pixbuf libffi cairosvg
$ cairosvg image.svg -o image.png

I got the idea from https://cairosvg.org/documentation/, which says that all its dependencies can be installed with WeasyPrint. WeasyPrint's documentation for installation on MacOSX at https://weasyprint.readthedocs.io/en/latest/install.html#macos says to get the dependencies from HomeBrew:

brew install python3 cairo pango gdk-pixbuf libffi

So I tried it with conda instead and it worked fine.

Vein answered 17/9, 2020 at 22:5 Comment(5)
Works nicely through conda. (Win10)Blouse
You saved my day bro! This answer is great and works!Loach
You saved my day bro! This answer is great and works!Loach
the only answer that fixes the issue on my M1 Pro!Rarity
Deserve the correct response! +1Matchbook
D
6

It seems cairo depends a shared library which is not in standard search library, however, the python is calling dlopen to dynamic load the library, so you could try to put the libcairo.so.2(if it's a link, then make sure the reference locates at the same folder) in the working directory. You can also try pkg-config to set the environment. see here http://people.freedesktop.org/~dbn/pkg-config-guide.html

Damon answered 29/1, 2015 at 10:0 Comment(2)
You're indeed correct. By putting the libcairo.so file in the working directory, the script could go further. However, it started to ask for more lib-files so I ended up chucking the whole folder there, didn't have to to figure out exactly which ones were needed.Eads
If you follow the link, and install the exe mentioned in the link with all the default options it would work. Note: Make sure you close and reopen the python terminal or jupyter notebook server.Lemmy
S
5

I just had the same issue ("OSError: cannot load library libcairo.so.2: error 0x7e"), and this is how I solved the problem on Windows (Windows 7 x64, Python 3.4.2 x86 (MSC v.1600 32 bit)):

  • downloaded an all-in-one bundle of the GTK+ stack including 3rd-party dependencies (which contains libcairo-2.dll and other Cairo-related libraries)
  • extracted this archive to a path which does NOT contain spaces (e.g. C:\Programs\gtk+)
  • added the extracted directory's bin subdirectory (which contains the mentioned libcairo-2.dll and other necessary files) to the PATH
    • Win+R, SystemPropertiesAdvanced
    • Environment Variables...
    • added this directory to the Path variable (either to the user variables or system variables, after a semicolon) (e.g. ...;C:\foo;C:\Programs\gtk+)
    • OK
  • pip install cairosvg
  • tested it with a very simple code, which already had worked:
import cairosvg
testsvg = '<svg height="30" width="30">\
    <text y="10">123</text>\
    </svg>'
svgConvertedToPng = cairosvg.svg2png(bytestring=testsvg)
print(svgConvertedToPng)
Spooner answered 12/5, 2015 at 10:18 Comment(1)
the link to the all in one bundle does not work anymoreDoublethink
S
4

Solved on Windows 10 as follows:

  1. download the headless UniConverter installer

  2. Find where it installed and add its dll subdirectory to the system path.

  3. Close and re-open the command window to get the updated path.

Squalor answered 22/6, 2020 at 17:0 Comment(0)
I
2

What I found for MAC OSX when searching the internet for a quick solution.

So after installation of the external library with homebrew:

brew install cairo #and other necessary stuff

library linking

ln -s /opt/homebrew/lib/libcairo.2.dylib .

Need to link lib to run the python code, it is working for me.

Impetuous answered 21/3, 2023 at 17:16 Comment(0)
R
0

The 64-bit GTK3 installer worked for me. In the installer, I kept the "Set up PATH environment variable" checkbox checked, as recommended in this otherwise outdated guide.

Not important: Before that, I tried the 32-bit GTK2 installer as recommended in the cairocffi documentation under "Installing cairo on Windows", but that resulted in cannot load library 'C:\Program Files (x86)\GTK2-Runtime\bin\libcairo-2.dll': error 0xc1, and then I found this WeasyPrint issue (WeasyPrint apparently uses cairo and is from the same company as cairocffi and CairoSVG), which led me to the documentation, which links to the 64-bit version linked above.

Roselleroselyn answered 10/12, 2023 at 11:7 Comment(0)
S
0

On Ubuntu/Raspberry Pi, the solution was to sudo apt install libcairo2 which brings in all the dependencies needed.

I'm adding this answer here as the original question is for Windows but the responses are mixed across OSes

Sestertium answered 7/4 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.