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

含EasyOCR的Python代码打包EXE后导入错误的解决及替代库咨询

问题解决:EasyOCR打包EXE导入错误及无外部依赖OCR库推荐

一、EasyOCR打包EXE的导入错误解决

问题场景

Python代码在PyCharm中运行正常,使用命令:

pyinstaller -F asd.py --collect-all easyocr

打包为EXE后出现依赖导入错误,测试代码及报错如下:

测试代码1

print("import1")
import time
print("import2")
from pynput import keyboard
print("import3")
import easyocr
print("import4")
import threading, datetime, os, gzip, psutil, pyautogui
print("import5")
from PIL import ImageGrab

报错信息1

import1
import2
import3
torchvision\io\image.py:13: UserWarning: Failed to load image Python extension: ''If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. Otherwise, there might be something wrong with your environment. Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?
torch_jit_internal.py:857: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x0000017E5EDE7F60>.
  warnings.warn(
torch_jit_internal.py:857: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x0000017E5EE08A40>.
  warnings.warn(
Cannot load imports from non-existent stub 'C:\\Users\\User\\AppData\\Local\\Temp\\_MEI24122\\skimage\\exposure\\__init__.pyi'

测试代码2

print("import1")
import time
print("import2")
from pynput import keyboard
import libpng, libjpeg
print("import3")
import torchvision.io
print("import4")
import skimage.exposure
print("import5")
import threading, datetime, os, gzip, psutil, pyautogui, easyocr
print("import6")
from PIL import ImageGrab

报错信息2

import1
import2
import3
torchvision\io\image.py:13: UserWarning: Failed to load image Python extension: ''If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. Otherwise, there might be something wrong with your environment. Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?
torch_jit_internal.py:857: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x0000026148901760>.
  warnings.warn(
torch_jit_internal.py:857: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x0000026148902200>.
  warnings.warn(

import4
Cannot load imports from non-existent stub 'C:\\Users\\User\\AppData\\Local\\Temp\\_MEI43802\\skimage\\exposure\\__init__.pyi'

解决方法

  • 修复skimage stub缺失:PyInstaller未自动收集scikit-image的stub文件,修改打包命令强制收集skimage模块:
    pyinstaller -F asd.py --collect-all easyocr --collect-all skimage
    
  • 处理torchvision警告:
    • 若无需torchvision的图像功能,直接在代码开头添加警告抑制:
      import warnings
      warnings.filterwarnings("ignore", category=UserWarning, module="torchvision")
      
    • 若需要相关功能,打包前确保系统已安装libjpeg和libpng依赖库,再重新打包。
  • 尝试非单文件模式:单文件打包可能存在临时路径解压问题,改用目录模式打包测试:
    pyinstaller -D asd.py --collect-all easyocr --collect-all skimage
    
  • 更新依赖版本:确保所有相关库为最新版,避免版本兼容问题:
    pip install --upgrade easyocr torchvision scikit-image pyinstaller
    

二、无外部依赖的轻量OCR库推荐(排除pytesseract类)

针对背景统一、识别需求简单的场景,推荐以下无需安装外部应用的库:

  • mOCR:纯Python实现的轻量印刷体OCR,无外部依赖,适合简单文本识别。
    安装:pip install mocr
    使用示例:

    from mocr import Mocr
    reader = Mocr()
    text = reader.recognize('test.png')
    print(text)
    
  • PaddleOCR轻量版:百度开源OCR,支持纯Python安装,轻量模型体积小、速度快,适合简单场景。
    安装:pip install paddlepaddle paddleocr
    使用示例(指定轻量模型):

    from paddleocr import PaddleOCR
    # 关闭角度分类、GPU加速,使用轻量模型
    ocr = PaddleOCR(use_angle_cls=False, lang='ch', use_gpu=False)
    result = ocr.ocr('test.png', cls=False)
    # 提取识别文本
    for line in result:
        print(line[1][0])
    
  • EasyOCR轻量配置:若坚持使用EasyOCR,可指定轻量模型并提前下载,减少打包依赖问题:

    import easyocr
    # 指定模型存储目录,提前下载好模型后打包
    reader = easyocr.Reader(['ch_sim', 'en'], model_storage_directory='./models', download_enabled=False)
    result = reader.readtext('test.png')
    for detection in result:
        print(detection[1])
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 03:57:53