You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller未分析paddle隐式导入,打包后exe运行报错求助

PyInstaller打包PaddleOCR脚本出现ModuleNotFoundError: No module named 'framework_pb2'问题

问题描述

Python脚本使用PaddleOCR功能运行正常,但通过PyInstaller打包成exe文件后,运行exe时报错,错误信息如下:

Traceback (most recent call last):
  File "mri_ocr_core.py", line 1, in <module>
  File "connect_backend_core.py", line 12, in init connect_backend_core
  File "main_core.py", line 17, in init main_core
  File "ocr2.py", line 35, in init ocr2
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddleocr\__init__.py", line 14, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddleocr\paddleocr.py", line 21, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\__init__.py", line 57, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\__init__.py", line 15, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\spawn.py", line 24, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\utils.py", line 28, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\fleet\__init__.py", line 20, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\fleet\base\fleet_base.py", line 31, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\fluid\ir.py", line 28, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\fluid\proto\pass_desc_pb2.py", line 16, in <module>
ModuleNotFoundError: No module named 'framework_pb2'
[31068] Failed to execute script 'mri_ocr_core' due to unhandled exception!

尝试将paddle.fluid.proto.framework_pb2添加到hiddenimports中重新打包,但PyInstaller的输出信息中未显示对该隐式导入进行分析,问题依旧存在。

环境版本信息

Python 3.7.9
paddle-bfloat==0.1.7
paddleocr==2.6.0.1
paddlepaddle==2.3.2
pyinstaller==5.6.2
pyinstaller-hooks-contrib==2022.2

问题复现步骤

  1. 安装Python 3.7.9
  2. 安装CPU版本paddlepaddle:
python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
  1. 安装指定版本PaddleOCR:
pip install "paddleocr>=2.6.0.3"
  1. 安装PyInstaller:
pip install pyinstaller
  1. 创建测试脚本test.py:
from paddleocr import PaddleOCR,draw_ocr
ocr = PaddleOCR()
img_path = r'./66.png'
result = ocr.ocr(img_path, cls=True)
for idx in range(len(result)):
    res = result[idx]
    for line in res:
        print(line)
  1. 执行打包命令:
pyinstaller test.py --noconfirm
  1. 运行生成的test.exe,出现如下错误:
Traceback (most recent call last):
  File "paddle\fluid\ir.py", line 24, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\fluid\proto\pass_desc_pb2.py", line 16, in <module>
ModuleNotFoundError: No module named 'framework_pb2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddleocr\__init__.py", line 14, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddleocr\paddleocr.py", line 21, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\__init__.py", line 62, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\__init__.py", line 15, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\spawn.py", line 24, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\utils\launch_utils.py", line 27, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\fleet\__init__.py", line 31, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\distributed\fleet\fleet.py", line 33, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\fluid\ir.py", line 28, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "paddle\fluid\proto\pass_desc_pb2.py", line 16, in <module>
ModuleNotFoundError: No module named 'framework_pb2'
[15880] Failed to execute script 'test' due to unhandled exception!
  1. 修改test.spec文件,添加hiddenimports:
hiddenimports = set()
hiddenimports = list(hiddenimports)
hiddenimports.extend(['paddle.fluid.proto.framework_pb2'])

a = Analysis(
    ['test.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=hiddenimports,
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
  1. 重新打包:
pyinstaller test.spec --noconfirm
  1. 查看PyInstaller输出信息,未发现对paddle.fluid.proto.framework_pb2模块的分析记录,exe运行仍报错。

内容的提问来源于stack exchange,提问作者Wai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 13:45:49