12

I'm getting OSError: dlopen() failed to load a library: cairo / cairo-2 when trying to execute Django after a new install. Using Windows.

I'm using Weasyprint, seems to be related to that one, according to the full trace:

Unhandled exception in thread started by <function wrapper at 0x03F605B0>
Traceback (most recent call last):
  File "C:\my-venv\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\my-venv\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\my-venv\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "C:\my-venv\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\my-venv\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\my-venv\lib\site-packages\django\apps\registry.py", line 115, in populate
    app_config.ready()
  File "C:\my-venv\lib\site-packages\debug_toolbar\apps.py", line 15, in ready
    dt_settings.patch_all()
  File "C:\my-venv\lib\site-packages\debug_toolbar\settings.py", line 243, in patch_all
    patch_root_urlconf()
  File "C:\my-venv\lib\site-packages\debug_toolbar\settings.py", line 231, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "C:\my-venv\lib\site-packages\django\core\urlresolvers.py", line 568, in reverse
    app_list = resolver.app_dict[ns]
  File "C:\my-venv\lib\site-packages\django\core\urlresolvers.py", line 360, in app_dict
    self._populate()
  File "C:\my-venv\lib\site-packages\django\core\urlresolvers.py", line 293, in _populate
    for pattern in reversed(self.url_patterns):
  File "C:\my-venv\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\my-venv\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\my-venv\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\my-venv\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\my-project\project\urls.py", line 9, in <module>
    from utils.views import site_info, nav_autocomplete
  File "C:\my-project\utils\views.py", line 24, in <module>
    import weasyprint
  File "C:\my-venv\lib\site-packages\weasyprint\__init__.py", line 338, in <module>
    from .css import PARSER, preprocess_stylesheet  # noqa
  File "C:\my-venv\lib\site-packages\weasyprint\css\__init__.py", line 30, in <module>
    from . import computed_values
  File "C:\my-venv\lib\site-packages\weasyprint\css\computed_values.py", line 18, in <module>
    from .. import text
  File "C:\my-venv\lib\site-packages\weasyprint\text.py", line 18, in <module>
    import cairocffi as cairo
  File "C:\my-venv\lib\site-packages\cairocffi\__init__.py", line 46, in <module>
    cairo = dlopen(ffi, 'cairo', 'cairo-2')
  File "C:\my-venv\lib\site-packages\cairocffi\__init__.py", line 43, in dlopen
    raise OSError("dlopen() failed to load a library: %s" % ' / '.join(names))
OSError: dlopen() failed to load a library: cairo / cairo-2
SaeX
  • 17,240
  • 16
  • 77
  • 97

3 Answers3

5

See http://win32builder.gnome.org/ or http://gtk-win.sourceforge.net/home/ for a list of GTK+ bundles to install. These include the required cairo/cairo-2 libs.

  • Extract the contents of the bin\ folder to e.g. C:\Program Files (x86)\GTK2-bundle\ (any directory would do)
  • Add the above directory to your PATH environment variable
  • Restart your development environment (in my case PyCharm)
SaeX
  • 17,240
  • 16
  • 77
  • 97
  • In troubleshooting, on windows, you really need to take care to avoid path conflicts in the environment variables. I had updated visual studio and installed another software suite, one of the updates changed some of my paths, causing this error to surface. – Bob Jordan Aug 15 '17 at 04:24
  • If you put the directory at the end of your PATH and you still get this error, try moving it closer to the front. I encountered DLL Hell when it was at the end. – drhagen Jun 22 '18 at 13:13
  • 2
    I've downloaded the package, extracted it to C:\Program Files (x86)\GTK 2\bin, and added C:\Program Files (x86)\GTK 2\ to the path variable. Still getting the same error, can anyone help? – Laurie Jun 28 '18 at 11:52
  • make sure you download the right version of GTK. For windows, check this. https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer – haojie Jan 10 '23 at 06:04
1

I got following error message: OSError: dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2, on Windows 10.

The GTK+ libraries which contain the libraries above can be obtained and installed from https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer, as documented on https://weasyprint.readthedocs.io/en/latest/install.html#gtk64installer.

Note that your development environment will need to be restarted to read PATH.

SaeX
  • 17,240
  • 16
  • 77
  • 97
0

I had same problem on windows X64/Python37 but i fixed with bellow link:

follow this steps : https://weasyprint.readthedocs.io/en/stable/install.html#windows, (i installed gtk with MSYS Installer and added C:\msys64\mingw32\bin to PATH variable,after that i occurred with same error and i deleted cairocffi python library and reinstall it)

Javad Asoodeh
  • 299
  • 3
  • 12