WeasyPrint: Installation problem on macOS M1 chip

I have Python already on macOS version 11.6 using Pyenv.

$ python --version
Python 3.9.6

I followed the doc for installation.

pip install weasyprint
brew install pango libffi

I get the following error.

 weasyprint --info
Traceback (most recent call last):
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/bin/weasyprint", line 5, in <module>
    from weasyprint.__main__ import main
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/lib/python3.9/site-packages/weasyprint/__init__.py", line 322, in <module>
    from .css import preprocess_stylesheet  # noqa isort:skip
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/lib/python3.9/site-packages/weasyprint/css/__init__.py", line 27, in <module>
    from . import computed_values, counters, media_queries
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/lib/python3.9/site-packages/weasyprint/css/computed_values.py", line 16, in <module>
    from ..text.ffi import ffi, pango, units_to_double
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/lib/python3.9/site-packages/weasyprint/text/ffi.py", line 380, in <module>
    gobject = _dlopen(
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/lib/python3.9/site-packages/weasyprint/text/ffi.py", line 377, in _dlopen
    return ffi.dlopen(names[0])  # pragma: no cover
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/lib/python3.9/site-packages/cffi/api.py", line 150, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/lib/python3.9/site-packages/cffi/api.py", line 832, in _make_ffi_library
    backendlib = _load_backend_lib(backend, libname, flags)
  File "/Users/shinichiokada/.pyenv/versions/3.9.6/lib/python3.9/site-packages/cffi/api.py", line 827, in _load_backend_lib
    raise OSError(msg)
OSError: cannot load library 'gobject-2.0-0': dlopen(gobject-2.0-0, 2): image not found.  Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'

I installed gtk3.

brew install gtk+3

But it doesn’t solve the problem.

How can I solve the problem?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I found the solution here

You need to run the following:

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

Now it returns some info:

$ weasyprint --info
Unable to revert mtime: /Library/Fonts
System: Darwin
Machine: arm64
Version: Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:20 PDT 2021; root:xnu-7195.141.6~3/RELEASE_ARM64_T8101
Release: 20.6.0

WeasyPrint version: 53.3
Python version: 3.8.10
Pydyf version: 0.1.1
Pango version: 14810

@liZe Do you have any references or more info on how to do that in a good way? I normally use pyenv.

For anyone else encountering this issue:

By default Python looks in /usr/local/lib for libraries with ctypes: https://github.com/python/cpython/blob/v3.10.0/Lib/ctypes/macholib/dyld.py#L22 - however Homebrew versions of Python patch this to look in /opt/homebrew/lib: https://github.com/Homebrew/homebrew-core/blob/af990d0d5e3d70bd52b1e9acf1b0636e74851cac/Formula/python@3.9.rb#L187 - which is something Python versions from pyenv are lacking.

Annoyingly the source for dyld.py suggests that setting DYLD_FALLBACK_LIBRARY_PATH as an environment variable could be a workaround for this. Sadly if you’re calling Python with #!/usr/bin/env from a script (eg. Django ./manage.py), macOS System Integrity Protection decides to remove such environment variables for security.

Ideally a patched Python is the best route for this, however failing that - symbolic links should work.

Hello!

Linking Hombrew libraries outside Homebrew is not a good solution in my opinion. You should use Homebrew’s Python instead of using your system’s version of Python (see https://github.com/Kozea/WeasyPrint/issues/1448#issuecomment-925559421).