Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django dlopen()未能加载库:cairo/cairo-2_Django - Fatal编程技术网

Django dlopen()未能加载库:cairo/cairo-2

Django dlopen()未能加载库:cairo/cairo-2,django,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_

我的看法是:

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和Cairocfi。我正在使用osx El Capitan。

我通过以下链接解决此问题:
在OSX EL CAPITAN上新安装的
weasyprint
上,我也遇到了同样的问题。 我就是这样解决的

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

brew install cairo pango gdk-pixbuf libxml2 libxslt libffi
完成后,我试图找到
cairo
安装的路径。在我的例子中,位置是
/usr/local/homebrew/cillar/cairo/1.14.6_1/lib/
我刚刚将它导出到我的DYLD库路径

export DYLD_LIBRARY_PATH=/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/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
在那之后,我再次尝试运行
weasyprint
,结果成功了

(test)anurag-mac:~ anurag$ weasyprint --version
WeasyPrint version 0.31
我希望其他人也觉得它有用

更新-1

尽管上述方法有效,但MySQL python开始为此给出错误,
culprit
正在定义回退库路径。所以我删除了这条线

export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
这又给了我一个
gobject
错误,然后我试图找到它的安装位置,并将其附加到
DYLD\u库路径中

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/

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

 sudo apt-get install libpango1.0-0
 sudo apt-get install libcairo2
 sudo apt-get install libpq-dev
在链接中查看依赖项:

我在macOS Mojave上也犯了同样的错误。我的解决方案是用
pip3
安装
cairocfi
,用
brew
安装
cairo
。这两个命令本身失败,但它们一起用于
box.py

pip3 install cairocffi
brew install cairo

如果在使用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
有关其他平台,请参阅以下链接


我还必须将
/opt/homebrew/cillar/fontconfig/2.13.1/lib/
添加到DYLD\u LIBRARY\u路径中
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