关于使用 pyinstaller 打包项目时,所抛出的关于 no module.... 。暴力解法 #11342
Replies: 13 comments 3 replies
-
2024年3月4日實測方法有效, 謝謝. |
Beta Was this translation helpful? Give feedback.
-
2024.3.27, 亲测有效, 另外如果打包后运行出现 YOUR_PYTHON_PATH/site-packages/paddle/fluid/core.py
|
Beta Was this translation helpful? Give feedback.
-
2024.7.17实测有效, 太感谢了 |
Beta Was this translation helpful? Give feedback.
-
2024/7/25实测有效 |
Beta Was this translation helpful? Give feedback.
-
Working Solution (08.08.2024) if an error exists in
|
Beta Was this translation helpful? Give feedback.
-
亲测有效,本人菜鸟水品,也是少哪个就复制哪个。 |
Beta Was this translation helpful? Give feedback.
-
2024/7/25实测有效,强! |
Beta Was this translation helpful? Give feedback.
-
感谢 @chenzhiron 的分享。我打包自己的脚本 我的环境是:
首先运行 # -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_all
# https://github.com/PaddlePaddle/PaddleOCR/discussions/11342#discussion-5925620
datas_ocr_and_rename = []
binaries_ocr_and_rename = []
hiddenimports_ocr_and_rename = []
for mod_name in ('paddleocr', 'pyclipper', 'skimage', 'imgaug', 'scipy.io', 'lmdb'): # 我没用到 imghdr 模块
tmp_ret = collect_all(mod_name)
datas_ocr_and_rename += tmp_ret[0]
binaries_ocr_and_rename += tmp_ret[1]
hiddenimports_ocr_and_rename += tmp_ret[2]
# 此处的 venv 路径需要改成你的 venv 路径
binaries_ocr_and_rename += [('.venv/Lib/site-packages/paddle/libs', 'paddle/libs')]
hiddenimports_ocr_and_rename += '_cffi_backend' # 这是我项目中需要的,如果你项目中没有应该删去这行
ocr_and_rename_a = Analysis(
['ocr_and_rename.py'],
pathex=['.'],
binaries=binaries_ocr_and_rename,
datas=datas_ocr_and_rename,
hiddenimports=hiddenimports_ocr_and_rename,
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)
ocr_and_rename_pyz = PYZ(ocr_and_rename_a.pure)
ocr_and_rename_exe = EXE(
ocr_and_rename_pyz,
ocr_and_rename_a.scripts,
[],
exclude_binaries=True,
name='ocr_and_rename',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
ocr_and_rename_exe,
ocr_and_rename_a.binaries,
ocr_and_rename_a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='ocr_and_rename',
) 最后运行 |
Beta Was this translation helpful? Give feedback.
-
Whether the OCR converts the PDF and other documents into Images then extract the data? OCR是否将PDF等文档转换为图像然后提取数据? |
Beta Was this translation helpful? Give feedback.
-
你们能指定这个opencv-contrib-python模块打包吗?这个模块我打包时总是说找不到文件 |
Beta Was this translation helpful? Give feedback.
-
在Kimi的协助下,花了一周时间终于打包成功 Paddle3.2.1 python3.12
OCRThunder.spec datas_OCRThunder = [] for mod_name in ('paddleocr', 'pyclipper', 'skimage', 'imgaug', 'scipy.io', 'lmdb'): paddlex 版本文件datas_OCRThunder += [('.venv/Lib/site-packages/paddlex/.version', 'paddlex')] OCR 配置文件datas_OCRThunder += collect_all('paddleocr')[0] Cython 动态源文件(Paddle 运行时需要)cython_utils = collect_all('Cython')[0] # 自动收集所有数据 模型文件(相对路径)datas_OCRThunder += [ pathex=[r'D:\PYQT.venv\Lib\site-packages'] Paddle 动态库binaries_OCRThunder += [('.venv/Lib/site-packages/paddle/libs', 'paddle/libs',)] 隐藏导入hiddenimports_OCRThunder += ['numpy', 'cv2', 'PIL', 'PyQt6','paddleocr', OCRThunder_a = Analysis( OCRThunder_pyz = PYZ(OCRThunder_a.pure) OCRThunder_exe = EXE( COLLECT( ========== 1️⃣ paddlex 基本资源 ==========datas, binaries, hiddenimports = collect_all('paddlex') 显式添加 paddlex 版本文件,保证 OCR pipeline 可用datas.append((os.path.join(os.path.dirname(paddlex.file), '.version'), 'paddlex')) ========== 2️⃣ paddleocr ==========do, bo, ho = collect_all('paddleocr') ========== 3️⃣ OCR 相关依赖 ==========ocr_hiddenimports = [ ========== 4️⃣ 常用科学计算库 ==========numpy & scipy 的 DLL 必须打包dn, bn, hn = collect_all('numpy') ds, bs, hs = collect_all('scipy') ) 必须在创建 pipeline 前必须先设置,再导入 create_pipelineimport paddlex.utils.deps paddlex.utils.deps._installed_extras = {"ocr": True} from paddlex.inference.pipelines import create_pipeline |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
简单的几个坑
仅供参考,本人python熟练程度仅限于菜鸟教程。
导入模块的错误
1. 使用默认打包 pyinstaller.exe -D .\main.py
main.exe启动后抛出了异常
问题查找:发现 pyinstaller 没有 把paddleOcr打包。
在issue上面查找了一番,发现官方描述是在打包命令后面加上 --collect-all paddleocr
还有其他参数,但是暂时不顶用解决办法:打包命令换成 pyinstaller.exe -D .\main.py --collect-all paddleocr
2. 执行错误一解决办法后,启动后又报错
错误查找:没找到模块。
一脸摸逼,这不是paddleocr 模块的内容吗?解决办法: 打包命令接着加 pyinstaller.exe -D .\main.py --collect-all paddleocr --collect-all pyclipper
3.执行错误二解决后,启动后又报错。
发现是同样的错误,直接按照错误二解决。
继续错误二解法,解决
继续抛出没引入模块错误,继续错误二解法,解决
继续,错误二解法,解决
继续,错误二解决
最终打包的命令,暴力解法
pyinstaller.exe -D .\main.py --collect-all paddleocr --collect-all pyclipper --collect-all imghdr --collect-all skimage --collect-all imgaug --collect-all scipy.io --collect-all lmdb
新错误
1.关于模型路径问题
2. .dll库 文件也没有打包进去
问题查找:发现 issue 已经有人解决过了,直接照抄


解决办法: 把 python 虚拟环境中的依赖项 paddle -> libs 文件夹内的文件 复制一份,粘贴到打包的项目的 paddle -> libs 文件夹内,全部替换即可
(题外话,我也不清楚为什么会这样)
打开打包后的 paddle 文件夹,可以发现 libs 文件夹下的文件比开发依赖项 paddle文件夹下的libs 文件要少
重新运行,成功。✿✿ヽ(°▽°)ノ✿
Beta Was this translation helpful? Give feedback.
All reactions