Paddle使用pyinstaller打包出错的解决方法

使用PyQt5开发了一个基于paddle ocr的文字识别程序,打包的时候报错了。给大家分享一下解决方法https://juejin.cn/post/7287562282669883451

  • 为了测试paddle模块的打包问题,所以当前demo.py只引用了paddleOCR模块
  • demo.py
python 复制代码
from paddleocr import PaddleOCR
def ocr_text(path):
    try:
        ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
        text = ocr.ocr(path, cls=True)
        result = []
        for t in text[0]:
            txt = t[1][0]
            result.append(txt)
        return result
    except:
        pass


if __name__ == '__main__':
    ocr_text("./screen.png")
步骤一:显式导入依赖项
  • 显式导入依赖项:在你的demo.py文件中,尝试显式导入项目的依赖项,例如paddle和framework_pb2,有助于帮助pyinstaller正确识别这些依赖项。在demo.py文件的顶部添加以导入语句:
python 复制代码
import paddle
from paddle.fluid.proto import framework_pb2
步骤二:pyinstaller的.spec文件中加入--add-data选项
  • 将paddle所需的文件包含进去。通常包括ocr的模型文件和其他依赖项。
  • --add-data选项将本地paddle路径path/to/paddle目录文件添加到生成的可执行文件中,使用;分割,./paddle代表的是生成文件的同级目录下的paddle文件中。
shell 复制代码
pyinstaller --add-data "path/to/paddle;./paddle" --add-data "path/to/paddleocr;./paddleocr" 
完成程序和打包脚本

demo.py

python 复制代码
from paddleocr import PaddleOCR
import paddle  # 新增
from paddle.fluid.proto import framework_pb2  # 新增


def ocr_text(path):
    try:
        ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
        text = ocr.ocr(path, cls=True)
        result = []
        for t in text[0]:
            txt = t[1][0]
            result.append(txt)
        return result
    except:
        pass


if __name__ == '__main__':
    ocr_text("./screen.png")
  • 打包处理
    • --add-data添加选项包到程序目录中,-F将生成一个单一的可执行文件。
shell 复制代码
pyinstaller -F demo.py --add-data "path/to/paddle;./paddle" --add-data "path/to/paddleocr;./paddleocr"
相关推荐
曲幽9 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
用户83562907805114 小时前
使用 C# 在 Excel 中创建数据透视表
后端·python
码路飞17 小时前
FastMCP 实战:一个 .py 文件,给 Claude Code 装上 3 个超实用工具
python·ai编程·mcp
dev派18 小时前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪20 小时前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户03321266636720 小时前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫1 天前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派1 天前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风1 天前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽1 天前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic