7

I installed several files based upon `https://pbpython.com/pdf-reports.htm to create reports. However the following error messages

Traceback (most recent call last):
  File "C:\histdata\test02.py", line 10, in <module>
    from weasyprint import HTML
  File "C:\Users\AquaTrader\AppData\Local\Programs\Python\Python38\lib\site-packages\weasyprint\__init__.py", line 322, in <module>
    from .css import preprocess_stylesheet  # noqa isort:skip
  File "C:\Users\AquaTrader\AppData\Local\Programs\Python\Python38\lib\site-packages\weasyprint\css\__init__.py", line 27, in <module>
    from . import computed_values, counters, media_queries
  File "C:\Users\AquaTrader\AppData\Local\Programs\Python\Python38\lib\site-packages\weasyprint\css\computed_values.py", line 16, in <module>
    from ..text.ffi import ffi, pango, units_to_double
  File "C:\Users\AquaTrader\AppData\Local\Programs\Python\Python38\lib\site-packages\weasyprint\text\ffi.py", line 380, in <module>
    gobject = _dlopen(
  File "C:\Users\AquaTrader\AppData\Local\Programs\Python\Python38\lib\site-packages\weasyprint\text\ffi.py", line 377, in _dlopen
    return ffi.dlopen(names[0])  # pragma: no cover
  File "C:\Users\AquaTrader\AppData\Local\Programs\Python\Python38\lib\site-packages\cffi\api.py", line 150, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "C:\Users\AquaTrader\AppData\Local\Programs\Python\Python38\lib\site-packages\cffi\api.py", line 832, in _make_ffi_library
    backendlib = _load_backend_lib(backend, libname, flags)
  File "C:\Users\AquaTrader\AppData\Local\Programs\Python\Python38\lib\site-packages\cffi\api.py", line 827, in _load_backend_lib
    raise OSError(msg)
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'

Any suggestions? Thanks in advance. (Please note that there is a similar issue on github which tells the individual to install GTK3.) Is this correct?

Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
quinn
  • 177
  • 1
  • 2
  • 13

3 Answers3

6

The error means that the gobject-2.0.0 library, which is part of GTK3+, cannot be found. Did you follow the installation instructions (https://doc.courtbouillon.org/weasyprint/stable/first_steps.html), which include installation of GTK3+? If no, do that. If yes, then the problem is, that the GTK3+ DLLs are not where Python is looking for them. For this, you need to add the directory containing the DLLs (e.g. C:\Program Files\GTK3-Runtime Win64\bin on Windows) to your PATH environment variable. That directory contains the relevant libgobject-2.0-0.dll library.

For Python 3.8+ and weasyprint 54+ you can manually set the path to your GTK3+ library with the environment variable WEASYPRINT_DLL_DIRECTORIES (documentation).

mad
  • 320
  • 1
  • 11
  • 1
    Thanks. When looking at the documentation to install gtk3+ it was just too much trouble, so I decided not to use weasyprint but a different package instead. Should I consider the problem solved so you can get credit? It's fine by me. – quinn Sep 23 '21 at 13:06
  • What package did you end up using instead? – Shahar Nov 18 '21 at 10:24
  • 1
    According to [this answer](https://stackoverflow.com/a/69816601/525036) and [the release notes](https://docs.python.org/3/whatsnew/3.8.html#bpo-36085-whatsnew), Python 3.8+ ignores the DLL dependencies on PATH for extension modules and DLLs loaded with ctypes on Windows. Is there a workaround for that? Otherwise your answer only works for Python up to 3.7. – Didier L Dec 09 '21 at 15:57
  • In the end I fixed the issue by removing Python 3.9 installed from the Microsoft Store and reinstalling from the official website (in version 3.10.1), based on [this answer](https://stackoverflow.com/a/69320292/525036). I – Didier L Dec 09 '21 at 16:25
1

Install GTK-for-Windows-Runtime-Environment-Installer from:

https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases

tno2007
  • 1,993
  • 25
  • 16
0

As @mad said, you need the GTK3 library to have the libobject-2.0.0 DLL. In Github Actions for example, you might be interested to use the tschoonj/GTK-for-Windows repository :

# Download GTK3 resources
git clone -b 2022-01-04 https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer GTK
cd GTK

# Add it to classic Windows install PATH
$Env:PATH += "$PWD\gtk-nsis-pack\bin"

# Add it to GitHub Actions PATH
# echo "$PWD\gtk-nsis-pack\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
secavfr
  • 628
  • 1
  • 9
  • 24