dlopen()无法加载库:cairo / cairo-2

29

这是我的观点:

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


    @staff_member_required
    def admin_order_pdf(request, order_id):
        order = get_object_or_404(Order, id=order_id)
        html = render_to_string('orders/order/pdf.html', {'order': order})
        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'filename="order_{}.pdf"'.format(order.id)
        weasyprint.HTML(string=html).write_pdf(response,
                                               stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + 'css/pdf.css')])
        return response

当我想导入时(import weasyprint),它会报错。

ERROR:

Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/orders/order/
Django Version: 1.8.6
Exception Type: OSError
Exception Value:    
dlopen() failed to load a library: cairo / cairo-2

我已经安装了weasyprint和cairocffi,我正在使用OSX El Capitan。

6个回答

37

16

我在安装最新版的weasyprint到EL CAPITAN的OSX系统上也遇到了同样的问题,以下是我的解决方法。

首先,当我通过pip安装时发现cairo未找到,因此我尝试使用homebrew命令安装如下:

brew install cairo pango gdk-pixbuf libxml2 libxslt libffi
一旦完成这一步骤,我尝试查找cairo安装路径。在我的情况下,位置是/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/,然后我将其导出到DYLD库路径中。
export DYLD_LIBRARY_PATH=/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/

然后我卸载并重新安装了weasyprint

pip uninstall weasyprint
pip install weasyprint

在那之后,我尝试运行weasyprint,但是出现了一个新的错误

Traceback (most recent call last):
File "/Users/anurag/VirtualEnvs/test/bin/weasyprint", line 11, in <module>
 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 <module>
 from .css import PARSER, preprocess_stylesheet  # noqa
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/css/__init__.py", line 30, in <module>
 from . import computed_values
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/css/computed_values.py", line 18, in <module>
 from .. import text
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/text.py", line 216, in <module>
 '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

我尝试查找object库的位置。在/opt/local/lib中找到并设置了备用库路径。

export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib

之后,我尝试再次运行weasyprint,结果成功了。

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

我希望其他人也能发现这对他们有用。

更新-1

尽管上述方法有效,但由于此原因,MySQL python开始出现错误并且罪魁祸首是定义备用库路径。 因此,我去掉了这行。

export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib

我遇到了 gobject 错误,然后尝试找到它的安装位置并将其添加到 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/

在执行该操作后,我遇到了一个类似于pango的错误。在纠正所有错误之后,这是最终可用的库路径。

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/

我还必须将/opt/homebrew/Cellar/fontconfig/2.13.1/lib/添加到DYLD_LIBRARY_PATH中。 - boxed
如果你遇到了 gobject-2.0-0 not found 的问题,可以参考这个链接:https://dev59.com/vVEG5IYBdhLWcg3wRIid。然后一切都应该正常工作! - gamingflexer

3

我在macOS Mojave上使用boxes.py时遇到了同样的错误。我的解决方法是使用pip3安装cairocffibrew安装cairo。这两个命令单独执行会失败,但一起使用可以让boxes.py正常工作:

pip3 install cairocffi
brew install cairo

1
如果您在使用weasyprint时遇到了这个错误,那么您可能忘记安装weasyprint的依赖项Pango、GdkPixbuf和cairo,这些依赖项无法使用pip/pip3安装。
对于Debian/Ubuntu:
sudo apt-get install build-essential python3-dev python3-pip python3-setuptools python3-wheel python3-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info

对于其他平台,请参考下面的链接

https://weasyprint.readthedocs.io/en/latest/install.html


0

-2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接