Python CFFI module fails when loading dll: OSError 0x7e
Asked Answered
P

2

7

I run Python 3.3 (Anaconda distribution) under Windows 7, 64-bit. I have attempted to install the Weasyprint app/library, which has a number of dependencies, including CFFI, which I had to compile from source because no compatible version of it was available in a binary distribution.

When I run weasyprint, it chokes during the import loading process, specifically when it calls CFFI in order to load the GTK+ library dll for Cairo. The error that I get is as follows:

$ weasyprint
Traceback (most recent call last):
  File "c:\anaconda\envs\py33\lib\site-packages\cffi-0.8-py3.3-win-amd64.egg\cffi\api.py", line 399, in _make_ffi_library
    backendlib = backend.load_library(name, flags)
OSError: cannot load library libcairo-2.dll: error 0x7e

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Anaconda\envs\py33\Scripts\weasyprint-script.py", line 9, in <module>
    load_entry_point('WeasyPrint==0.20', 'console_scripts', 'weasyprint')()
  File "C:\Anaconda\envs\py33\lib\site-packages\pkg_resources.py", line 343, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "C:\Anaconda\envs\py33\lib\site-packages\pkg_resources.py", line 2355, in load_entry_point
    return ep.load()
  File "C:\Anaconda\envs\py33\lib\site-packages\pkg_resources.py", line 2061, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "c:\anaconda\envs\py33\lib\site-packages\weasyprint-0.20-py3.3.egg\weasyprint\__init__.py", line 309, in <module>
    from .css import PARSER, preprocess_stylesheet
  File "c:\anaconda\envs\py33\lib\site-packages\weasyprint-0.20-py3.3.egg\weasyprint\css\__init__.py", line 30, in <module>
    from . import computed_values
  File "c:\anaconda\envs\py33\lib\site-packages\weasyprint-0.20-py3.3.egg\weasyprint\css\computed_values.py", line 18, in <module>
    from .. import text
  File "c:\anaconda\envs\py33\lib\site-packages\weasyprint-0.20-py3.3.egg\weasyprint\text.py", line 18, in <module>
    import cairocffi as cairo
  File "c:\anaconda\envs\py33\lib\site-packages\cairocffi-0.5.1-py3.3.egg\cairocffi\__init__.py", line 39, in <module>
    cairo = dlopen(ffi, 'libcairo-2.dll', 'cairo', 'libcairo-2')
  File "c:\anaconda\envs\py33\lib\site-packages\cairocffi-0.5.1-py3.3.egg\cairocffi\__init__.py", line 34, in dlopen
    return ffi.dlopen(names[0])  # pragma: no cover
  File "c:\anaconda\envs\py33\lib\site-packages\cffi-0.8-py3.3-win-amd64.egg\cffi\api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "c:\anaconda\envs\py33\lib\site-packages\cffi-0.8-py3.3-win-amd64.egg\cffi\api.py", line 405, in _make_ffi_library
    backendlib = backend.load_library(path, flags)
OSError: cannot load library C:\Windows\system32\libcairo-2.dll: error 0x7e 

The environment I have is as follows: Windows 7.1 64-bit, Python 3.3 64 bit, CFFI compiled (by me) under Visual Studio 2010 with a 64-bit environment, and Cairo's libcairo-2.dll also in a 64-bit version.

I am not a Windows programmer, and am only delving into this mess because I want to get Weasyprint to work for another (Python language) project. I have done a minor bit of Windows programming a long time ago under Delphi, so I have a vague grasp of how this stuff works, but I have been unable to solve this problem.

Prot answered 24/11, 2013 at 11:38 Comment(2)
Did you solve the problem at the end? I am facing the same error and have no idea where to start fixing it. Any suggestion is appreciated. Thank you!Dominant
The problem is that CFFI has to be compiled with Visual Studio, because the DLLs have to be compatible with Python, which was compiled under that toolchain. I had VS 2010 Express, which comes with C++, but CFFI was designed to be compiled under VS 2007, if I remember correctly, and Microsoft changed its make system so you can't use vsbuild anymore (or something like that that was equally annoying). Apparently the MS world isn't like the *nix world, where your toolchain from 2014 still pretty much compiles code from 1986. So I was SOL... I run weasyprint on a Mac box to get around the issue.Prot
S
2

I was getting similar errors (conflicting DLLs) and it was finally resolved simply by moving the path to GTK (ex: "C:\gtk\bin") to the beginning of my environment variables.

Spirketing answered 17/7, 2014 at 2:0 Comment(5)
And don't try to use "C:\gtk+\bin". It doesn't like spaces or the + in the path.Justajustemilieu
This didn't work for me. I still get the error 'OSError: dlopen() failed to load a library: cairo / cairo-2'Resect
@Resect - my guess is you're missing some dependencies. I think I used this tool dependencywalker.com to find some things I was missing. I think the versions that worked for me were pycairo-1.8.10.win32-py2.7.exe and gtk+-bundle_2.22.1-20101229_win64.zip . Hope it helps.Spirketing
thanks for the dependency walker suggestion. I got this output when selecting weasyprint.exe Error: At least one required implicit or forwarded dependency was not found. < Warning: At least one delay-load dependency module was not found. Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module. > I'm not sure how to interpret thatResect
Thanks, this helped. I think the issue was a space in my install directory (but I also moved the path to the beginning of my environment variables), annoying how fragile this is.Udo
A
0

Installing GTK+ didn't work for me. I solved this problem using UniConverter2.0. My environments is

  • Python 3.7
  • Windows 10 x64
  1. Install uniconvertor-2.0rc4-win64_headless.msi,
  2. Find the "dll" sub-directory under the UniConverter installation path.(In my case, C:\Program Files\UniConvertor-2.0rc4\dlls)
  3. Add this "dll" path to the system path.
  4. Close VSCode(or other) and reopen the project.
  5. Try to run your code again. Enjoy!
Abisia answered 13/7, 2020 at 2:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.