【epub2pdf 转换器】OSError: cannot load library ‘libgobject-2.0-0’: dlopen
文章目录
- 【epub2pdf 转换器】OSError: cannot load library 'libgobject-2.0-0': dlopen
- 报错信息:
- 解决:
- 方案 A(推荐,conda-forge 一把梭)
- 方案 B(用 Homebrew 装依赖,但要处理库搜索路径)
- 额外提醒:不要混用不同架构的库(Apple Silicon 常见坑)
报错信息:
----- WeasyPrint could not import some external libraries. Please carefully follow the installation steps before reporting an issue: https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#troubleshooting ----- Traceback (most recent call last): File "/Users/bytedance/ai/FreeBooks/epub2pdf_weasyprint.py", line 16, in <module> from weasyprint import HTML, CSS File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/weasyprint/__init__.py", line 440, in <module> from .css import preprocess_stylesheet # noqa: I001, E402 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/weasyprint/css/__init__.py", line 30, in <module> from ..text.fonts import FontConfiguration File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/weasyprint/text/fonts.py", line 17, in <module> from .constants import ( # isort:skip File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/weasyprint/text/constants.py", line 5, in <module> from .ffi import pango File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/weasyprint/text/ffi.py", line 476, in <module> gobject = _dlopen( ^^^^^^^^ File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/weasyprint/text/ffi.py", line 464, in _dlopen return ffi.dlopen(names[0], flags) # pragma: no cover ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/cffi/api.py", line 150, in dlopen lib, function_cache = _make_ffi_library(self, name, flags) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/cffi/api.py", line 834, in _make_ffi_library backendlib = _load_backend_lib(backend, libname, flags) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/envs/freebooks/lib/python3.12/site-packages/cffi/api.py", line 829, in _load_backend_lib raise OSError(msg) OSError: cannot load library 'libgobject-2.0-0': dlopen(libgobject-2.0-0, 0x0002): tried: 'libgobject-2.0-0' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibgobject-2.0-0' (no such file), '/opt/miniconda3/envs/freebooks/bin/../lib/libgobject-2.0-0' (no such file), '/usr/lib/libgobject-2.0-0' (no such file, not in dyld cache), 'libgobject-2.0-0' (no such file). Additionally, ctypes.util.find_library() did not manage to locate a library called 'libgobject-2.0-0'解决:
这个报错不是 Python 代码问题,而是WeasyPrint 依赖的系统动态库没装/没被当前环境找到。libgobject-2.0-*属于GLib/GObject(WeasyPrint 还依赖 pango/cairo 等)。
你现在是在conda 环境里用pip install weasyprint,但 conda 环境里并没有这些原生库,所以dlopen找不到libgobject-2.0-0。
方案 A(推荐,conda-forge 一把梭)
在当前环境中用 conda-forge 安装 WeasyPrint(会把 glib/pango/cairo 等依赖一起装好),并避免 pip 版冲突:
conda activate freebooks# 建议先卸载 pip 装的pip uninstall -y weasyprint# 用 conda-forge 安装(推荐)condainstall-c conda-forge weasyprint装完验证:
python -c"from weasyprint import HTML; print('ok')"如果你不想装完整 weasyprint 包,也可以只补依赖(但更容易踩坑):
condainstall-c conda-forge glib pango cairo gdk-pixbuf libffi harfbuzz fontconfig freetype方案 B(用 Homebrew 装依赖,但要处理库搜索路径)
如果你坚持pip install weasyprint,那就在 macOS 上用 brew 补齐依赖:
brewinstallglib pango cairo gdk-pixbuf libffi harfbuzz fontconfig freetype然后确保运行时能找到 brew 的.dylib(在 conda 里经常找不到),你可以临时加:
exportDYLD_FALLBACK_LIBRARY_PATH="$(brew --prefix)/lib:$DYLD_FALLBACK_LIBRARY_PATH"python -c"from weasyprint import HTML; print('ok')"额外提醒:不要混用不同架构的库(Apple Silicon 常见坑)
你是/opt/miniconda3(大概率 arm64),如果 brew 在 Rosetta 下装成了 x86_64,动态库也会加载失败。可以分别看:
python -c"import platform; print(platform.machine())"brew config|grepCPU如果你愿意,把下面两条输出贴出来,我可以帮你判断你该用 A 还是 B、以及是否存在架构混装问题:
conda info|grep-E"active env|platform"python -c"import platform; print(platform.platform(), platform.machine())"