【问题标题】:OSError: cannot load library 'gobject-2.0': error 0x7eOSError:无法加载库“gobject-2.0”:错误 0x7e
【发布时间】:2020-12-06 12:37:15
【问题描述】:

我按照Installing weasyprint(Django 项目)的说明安装了包weasyprint。 我的系统:win 10。我已经安装了 gtk3,它存在于我的 PATH

import weasyprint
...
@staff_member_required
def order_admin_pdf(request, order_id):
    # Получаем заказ по ID:
    order = get_object_or_404(Order, id=order_id)
    # Передаем объект в функцию render_to через генерацию шаблона pdf.html HTML в виде строки:
    html = render_to_string('shop/orders/order_admin_pdf.html',
                            {'order': order})
    # Создаем объект овтета с типом содержимого application/pdf и заголовком Content-Disposition:
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename=order_{}.pdf"'.format(order.id)
    # Вызов метода weasyprint для получения PDF документа:
    weasyprint.HTML(string=html).write_pdf(response,
                                           stylesheets=[weasyprint.CSS(
                                               settings.STATIC_ROOT + 'css/pdf.css')])
    return response

OSError:无法加载库“gobject-2.0”:错误 0x7e。此外,ctypes.util.find_library() 无法找到名为“gobject-2.0”的库

【问题讨论】:

标签: python django windows lib weasyprint


【解决方案1】:

我绝望了,决定安装库 gtk2 C:\Program Files (x86)\GTK2\lib\ 并在 PATH 列表中指定第一个。它工作......但我的操作系统 - 赢得 10 x64。为什么库 GTK3 拒绝工作,我不知道。

【讨论】:

    【解决方案2】:

    Python 3.8 开始,Windows 上扩展模块和使用ctypes 加载的 DLL 的 DLL 依赖关系现在得到更安全的解析。仅搜索系统路径、包含 DLL 或 PYD 文件的目录以及添加了add_dll_directory() 的目录以查找加载时依赖项。 具体来说,PATH 和当前工作目录不再使用,对它们的修改将不再对正常的 DLL 解析产生任何影响。

    如果您按照official documentation 中的安装指南进行操作,则以下示例有效。

    import os
    
    os.add_dll_directory(r"C:\Program Files\GTK3-Runtime Win64\bin")
    
    from weasyprint import HTML
    
    HTML('https://weasyprint.org/').write_pdf('weasyprint-website.pdf')
    

    本质上,您需要在与 WeasyPrint 交互之前致电 add_dll_directory()

    【讨论】:

    • 这个问题是很久以前提出的,从那以后我的理解发生了很大变化)这个答案更适合解决这个问题。
    • 谢谢。我遇到了类似的问题
    猜你喜欢
    • 2017-09-12
    • 2020-12-30
    • 2021-11-03
    • 1970-01-01
    • 2021-11-04
    • 2022-08-05
    • 2023-02-10
    • 2019-05-12
    相关资源
    最近更新 更多