Cane's Blog

Cane

【Problem】Pyinstaller 打包 Pydantic V2 失败

140
2023-12-11

问题描述

AttributeError: Failed to retrieve attribute compiled from module pydantic

详细日志

(spider) λ pyinstaller --onefile --name spider main.py

Traceback (most recent call last):
....
....
....
importlib_load_source
    mod_loader.exec_module(mod)
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\Cane\.conda\envs\spider\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pydantic.py", line 22, in <module>
    is_compiled = get_module_attribute('pydantic', 'compiled') in {'True', True}
  File "C:\Users\Cane\.conda\envs\spider\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 284, in get_module_attribute
    raise AttributeError(f"Failed to retrieve attribute {attr_name} from module {module_name}") from e
AttributeError: Failed to retrieve attribute compiled from module pydantic

问题原因

PyInstaller 不兼容 Pydantic V2 版本,使用了一些在 V2 版本已被移除的 V1 版本中的方法。

解决方案

更新 PyInstaller 的钩子贡献库(pyinstaller-hooks-contrib)到最新版本。

pip install -U pyinstaller-hooks-contrib