To resolve the "OSError: cannot load library 'gobject-2.0-0': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate 'gobject-2.0-0' as a library" error, you can try the following solutions:
sudo apt-get install libglib2.0-0 (for Debian/Ubuntu)
sudo yum install glib2-devel (for CentOS/RHEL)
sudo zypper install glib2-devel (for openSUSE)
export LD_LIBRARY_PATH=/path/to/library/directory:$LD_LIBRARY_PATH
sudo apt-get --reinstall install libglib2.0-0 (for Debian/Ubuntu)
sudo yum reinstall glib2-devel (for CentOS/RHEL)
sudo zypper reinstall glib2-devel (for openSUSE)
When using WeasyPrint on Windows, follow these steps:
# Insert the GTK3 Runtime folder at the beginning. Can be bin or lib, depending on the 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', '')
In Python 3.8, DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory()
are searched for load-time dependencies. Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution.
If you followed the installation guide from the official documentation, then the following example works:
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')
In essence, you need to call add_dll_directory()
before interacting with WeasyPrint.
Download and install MSYS2 from https://www.msys2.org/.
a) Install the GTK package and Python bundles from the MSYS2 terminal:
pacman -S mingw-w64-x86_64-gtk3
b) Install the Python GObject package:
pacman -S mingw-w64-x86_64-python-gobject
Update your $XDG_DATA_HOME
and $XDG_DATA_DIRS
to the installed path, for example: C:/msys64/mingw64/share
.
Reboot your system and check, it should work.