【问题标题】:dlopen() failed to load a library: cairo / cairo-2dlopen() 加载库失败:cairo / cairo-2
【发布时间】:2020-11-24 21:48:36
【问题描述】:

这是我的看法:

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。

【问题讨论】:

    标签: django


    【解决方案1】:

    通过在 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

    【讨论】:

      【解决方案2】:

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

      首先,cairo 通过 pip 安装时没有找到,所以我尝试使用以下命令通过 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
      

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

      UPDATE-1

      虽然上述方法有效,但 MySQL python 开始报错,culprit 正在定义一个备用库路径。所以我删除了这一行

      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
      【解决方案3】:

      我在 macOS Mojave 上使用 boxes.py 时遇到了同样的错误。我的解决方案是安装cairocffi 和pip3 和cairo 和brew。这两个命令单独失败,但它们一起为boxes.py 工作:

      pip3 install cairocffi
      brew install cairo
      

      【讨论】:

        【解决方案4】:

        如果您在使用 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】:

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

          【讨论】:

            【解决方案6】:
            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

            【讨论】:

              猜你喜欢
              • 2019-05-12
              • 1970-01-01
              • 1970-01-01
              • 2016-11-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多