30

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.

sophros
  • 14,672
  • 11
  • 46
  • 75
Niddro
  • 1,705
  • 2
  • 12
  • 24
  • 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. –  Jan 29 '15 at 09:49
  • 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? – Niddro Jan 29 '15 at 09:51

6 Answers6

41

On Mac OS X using homebrew:

brew install cairo
brew install pango
Hexatonic
  • 2,251
  • 2
  • 21
  • 26
9

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.

Harry Howard
  • 91
  • 1
  • 1
5

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

Cui Heng
  • 1,265
  • 8
  • 10
  • 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. – Niddro Jan 29 '15 at 12:55
  • 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. – Hissaan Ali Apr 06 '21 at 10:19
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)
Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
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.

Handcraftsman
  • 6,863
  • 2
  • 40
  • 33
1

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.

ati ince
  • 111
  • 1
  • 6