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

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

  • 为了测试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将生成一个单一的可执行文件,这里导入了项目中用到的paddle和paddleocr两个包。
shell 复制代码
pyinstaller -F demo.py --add-data "path/to/paddle;./paddle" --add-data "path/to/paddleocr;./paddleocr"
  • 最后打包成功
相关推荐
曲幽2 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户8356290780516 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon8 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly8 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程8 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly10 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风16 小时前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风16 小时前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
ZhengEnCi1 天前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风2 天前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python