Python: OSError: cannot load library libcairo.so.2
Question:
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:Python34libsite-packagescairocffi__init__.py", line 41, in <modul
e>
cairo = dlopen(ffi, *CAIRO_NAMES)
File "C:Python34libsite-packagescairocffi__init__.py", line 34, in dlopen
return ffi.dlopen(names[0]) # pragma: no cover
File "C:Python34libsite-packagescffiapi.py", line 118, in dlopen
lib, function_cache = _make_ffi_library(self, name, flags)
File "C:Python34libsite-packagescffiapi.py", line 411, in _make_ffi_libra
ry
backendlib = _load_backend_lib(backend, libname, flags)
File "C:Python34libsite-packagescffiapi.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.
Answers:
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
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:Programsgtk+
)
- 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:Programsgtk+
)
- 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)
On Mac OS X using homebrew:
brew install cairo
brew install pango
Solved on Windows 10 as follows:
-
download the headless UniConverter installer
-
Find where it installed and add its dll subdirectory to the system path.
-
Close and re-open the command window to get the updated path.
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.
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
ln -s /opt/homebrew/lib/libcairo.2.dylib .
Need to link lib to run the python code, it is working for me.
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:Python34libsite-packagescairocffi__init__.py", line 41, in <modul
e>
cairo = dlopen(ffi, *CAIRO_NAMES)
File "C:Python34libsite-packagescairocffi__init__.py", line 34, in dlopen
return ffi.dlopen(names[0]) # pragma: no cover
File "C:Python34libsite-packagescffiapi.py", line 118, in dlopen
lib, function_cache = _make_ffi_library(self, name, flags)
File "C:Python34libsite-packagescffiapi.py", line 411, in _make_ffi_libra
ry
backendlib = _load_backend_lib(backend, libname, flags)
File "C:Python34libsite-packagescffiapi.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.
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
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:Programsgtk+
) - added the extracted directory’s
bin
subdirectory (which contains the mentionedlibcairo-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:Programsgtk+
) - OK
- Win+R,
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)
On Mac OS X using homebrew:
brew install cairo
brew install pango
Solved on Windows 10 as follows:
-
download the headless UniConverter installer
-
Find where it installed and add its dll subdirectory to the system path.
-
Close and re-open the command window to get the updated path.
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.
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
ln -s /opt/homebrew/lib/libcairo.2.dylib .
Need to link lib to run the python code, it is working for me.