OSError:无法加载库“gobject-2.0”:错误0x7e

问题描述 投票:0回答:6

我按照说明安装了包weasyprint安装weasyprint(Django项目)。 我的系统: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
6个回答
9
投票

Python 3.8 开始,现在可以更安全地解决 Windows 上使用

ctypes
加载的扩展模块和 DLL 的 DLL 依赖关系。仅搜索系统路径、包含 DLL 或 PYD 文件的目录以及添加
add_dll_directory()
的目录以查找加载时依赖项。 具体来说,不再使用 PATH 和当前工作目录,对这些的修改将不再对正常的 DLL 解析产生任何影响。

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

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()


5
投票

我绝望了,决定安装 gtk2 库

C:\Program Files (x86)\GTK2\lib\
并指定 PATH 列表中的第一个。它有效......但我的操作系统 - win 10 x64。为什么 GTK3 库拒绝工作,我不知道。


2
投票

对于 MacOS 这很有帮助。基本上你必须创建几个链接。

sudo ln -s /opt/homebrew/opt/glib/lib/libgobject-2.0.0.dylib /usr/local/lib/gobject-2.0
sudo ln -s /opt/homebrew/opt/pango/lib/libpango-1.0.dylib /usr/local/lib/pango-1.0
sudo ln -s /opt/homebrew/opt/harfbuzz/lib/libharfbuzz.dylib /usr/local/lib/harfbuzz
sudo ln -s /opt/homebrew/opt/fontconfig/lib/libfontconfig.1.dylib /usr/local/lib/fontconfig-1
sudo ln -s /opt/homebrew/opt/pango/lib/libpangoft2-1.0.dylib /usr/local/lib/pangoft2-1.0

https://github.com/Kozea/WeasyPrint/issues/1556#issuecomment-1097977671


1
投票

对我来说,这是关于环境变量设置不正确。

我必须在 Windows 中添加 GTK3 作为系统环境变量才能使我的项目正常工作(Windows 10)。


0
投票

我使用 weasyprint 库制作了一个适用于 Windows 10 的 django 网站。还安装了适用于 Windows 运行时环境的 GTK+(版本 3)。抛出了同样的错误。

我尝试交换path中的路径,将windows更新到11,但没有帮助。 然后我将'sitecustomize.py'像Tontynahttps://github.com/Kozea/WeasyPrint/issues/971#issuecomment-544195744粘贴到虚拟环境文件夹'/Lib/site-packages',

发生另一个错误:字体配置错误:无法加载默认配置文件。

事实证明,正在加载“C:\Users naconda3 ontconfig.dll”库。

在文件“sitecustomize.py”中,我更改了这一行:

os.environ['PATH'] = GTK_FOLDER + os.pathsep + os.environ.get('PATH', '') 
到行:
os.environ['PATH'] = GTK_FOLDER
它有效


0
投票

对我有用的解决方案:

  1. 按照
    weasyprint
    在Windows上的使用,下载并安装GTK。
  2. 在Python的开头添加GTK(在本例中是
    python manage.py runserver
    ):
# insert the GTK3 Runtime folder at the beginning. Can be bin or lib, depending on path you choose while installing.
GTK_FOLDER = r'C:\Program Files\GTK3-Runtime Win64\bin'
os.environ['PATH'] = GTK_FOLDER + os.pathsep + os.environ.get('PATH', '')
© www.soinside.com 2019 - 2024. All rights reserved.