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

时间:2016-05-25 12:24:03

标签: django

这是我的观点:

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

当我想导入(导入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 个答案:

答案 0 :(得分:16)

通过在Ubuntu上安装以下依赖项来解决这个问题:

 sudo apt-get install libpango1.0-0
 sudo apt-get install libcairo2
 sudo apt-get install libpq-dev

查看链接中的依赖项:

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

答案 1 :(得分:8)

我在OSX EL CAPITAN上全新安装weasyprint也遇到了同样的问题。 这就是我解决它的方法。

首先,通过pip安装时找不到cairo,所以我尝试使用以下命令通过自制程序安装它

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

我试图找出对象库的位置。在/ opt / local / lib中找到它并设置回退库路径

export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib

之后,我尝试再次运行weasyprint并且工作正常

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

我希望其他人也觉得它很有用。

<强> UPDATE-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/

答案 2 :(得分:2)

在macOS Mojave上,boxes.py出现了相同的错误。我的解决方案是使用cairocffi安装pip3并使用cairo安装brew。这两个命令本身会失败,但是它们一起可以为boxes.py工作:

pip3 install cairocffi
brew install cairo

答案 3 :(得分:0)

我通过以下链接解决了这个问题: https://github.com/Kozea/WeasyPrint/issues/79

答案 4 :(得分:0)

如果在使用weasyprint时遇到此错误,则可能忘记安装无法使用pip / pip3安装的weasyprint的Pango,GdkPixbuf和cairo依赖项

对于Debain / 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

答案 5 :(得分:-1)

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

http://weasyprint.readthedocs.io/en/latest/install.html#linux