"oserror: cannot load library 'gobject-2.0-0': error 0x7e. additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'"

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:

  1. This error indicates that the system is unable to locate the 'gobject-2.0-0' library, which is typically part of the GObject library used in GTK-based applications.
  2. First, ensure that the GObject library is installed on your system. You can typically install it using your system's package manager:
  3. 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)
  4. After installing the GObject library, ensure that the library is available in the system's library search path. You may need to update the LD_LIBRARY_PATH environment variable to include the directory containing the library:
  5. export LD_LIBRARY_PATH=/path/to/library/directory:$LD_LIBRARY_PATH
  6. If you're still encountering the error, it's possible that the library file itself is missing or corrupted. Try reinstalling the GObject library:
  7. 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)
  8. Additionally, ensure that any symbolic links or aliases pointing to the library are correctly set up.
  9. If the error persists, consider checking the application's dependencies and requirements to ensure compatibility with the system's libraries and versions.

When using WeasyPrint on Windows, follow these steps:

  1. Download and install GTK.
  2. Add GTK to the beginning of Python (in this case, Python manage.py runserver):
# 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.