dlopen() failed to load a library: cairo / cairo-2

后端 未结 6 1079
走了就别回头了
走了就别回头了 2020-12-18 18:43

This is my view:

from django.conf import settings
from django.http import HttpResponse
from django.template.loader import render_to_string
import weasyprint
         


        
6条回答
  •  -上瘾入骨i
    2020-12-18 19:08

    I also had the same issue on a fresh installation of weasyprint on OSX EL CAPITAN. This is how I solved it.

    Firstly, cairo was not found by when installed via pip, so I tried installing it via homebrew using the following command

    brew install cairo pango gdk-pixbuf libxml2 libxslt libffi
    

    Once this is done, I tried to find out the path of cairo installation. For my case, the location was /usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/ I just exported this to my DYLD library path

    export DYLD_LIBRARY_PATH=/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/
    

    Then I uninstalled and installed weasyprint again

    pip uninstall weasyprint
    pip install weasyprint
    

    Post that, I tried to run weasyprint, but got a new error

    Traceback (most recent call last):
    File "/Users/anurag/VirtualEnvs/test/bin/weasyprint", line 11, in 
     load_entry_point('WeasyPrint==0.31', 'console_scripts', 'weasyprint')()
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/pkg_resources/__init__.py", line 565, in load_entry_point
     return get_distribution(dist).load_entry_point(group, name)
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2598, in load_entry_point
     return ep.load()
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2258, in load
     return self.resolve()
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2264, in resolve
     module = __import__(self.module_name, fromlist=['__name__'], level=0)
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/__init__.py", line 338, in 
     from .css import PARSER, preprocess_stylesheet  # noqa
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/css/__init__.py", line 30, in 
     from . import computed_values
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/css/computed_values.py", line 18, in 
     from .. import text
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/text.py", line 216, in 
     'libgobject-2.0.dylib')
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/text.py", line 212, in dlopen
     return ffi.dlopen(names[0])  # pragma: no cover
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/cffi/api.py", line 139, in dlopen
     lib, function_cache = _make_ffi_library(self, name, flags)
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/cffi/api.py", line 770, in _make_ffi_library
     backendlib = _load_backend_lib(backend, libname, flags)
    File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/cffi/api.py", line 759, in _load_backend_lib
     return backend.load_library(name, flags)
    OSError: cannot load library gobject-2.0: dlopen(gobject-2.0, 2): image not found
    

    I tried to find out the location of object library. Found it in /opt/local/lib and set the fallback library path

    export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
    

    After that, I tried running weasyprint again, and it worked

    (test)anurag-mac:~ anurag$ weasyprint --version
    WeasyPrint version 0.31
    

    I hope someone else also finds it useful.

    UPDATE-1

    Although the above method worked, MySQL python started giving errors because of this and culprit was defining a fallback library path. So I removed this line

    export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
    

    which gave me the gobject error again, then I tried finding the location of its installation and appended to the DYLD_LIBRARY_PATH

    export DYLD_LIBRARY_PATH=/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/:/usr/local/homebrew/Cellar/glib/2.48.2/lib/
    

    After doing that, I got a similar error for pango. After correcting all the errors, this is the final library path which worked

    export DYLD_LIBRARY_PATH=/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/:/usr/local/homebrew/Cellar/glib/2.48.2/lib/:/usr/local/homebrew/Cellar/pango/1.40.3/lib/
    

提交回复
热议问题