Python之pyinstaller打包exe填坑总结

一、起因

编写了一个提取图片中文字的python脚本,想传给同事使用,但是同事电脑上没有任何python环境,更没有安装python库,因此想到通过pyinstaller打包成exe程序传给同事使用,于是开始了不断地挖坑填坑之旅

import pytesseract
from PIL import Image
import tkinter
import tkinter.messagebox
from tkinter import filedialog

def select_file():
    file_path = filedialog.askopenfilename()
    if file_path:
        var_imagepath.set(file_path)  # Update the entry widget with the selected file path
        but_tiqu.config(state=tkinter.NORMAL)  # Enable the "提取文字" button

def extract_ocr_text(image_path):
    try:
        image = Image.open(image_path)
        text = pytesseract.image_to_string(image)
        return text
    except Exception as e:
        return f"Error: {str(e)}"
    
def cancel():
    var_imagepath.set('') 
    winoutput.delete("1.0","end")

win = tkinter.Tk()
win.geometry("1100x550")
win.title("图片文字提取工具")

var_imagepath = tkinter.StringVar()


# 展示数据
labimage = tkinter.Label(win, text='图片路径', width=80)
laboutput = tkinter.Label(win, text='提取结果', width=80)
entimagepath = tkinter.Entry(win, width=200, textvariable=var_imagepath)
winoutput = tkinter.Text(win, width=200, height=300)
scrollbar = tkinter.Scrollbar(win, command=winoutput.yview)
winoutput.config(yscrollcommand=scrollbar.set)

but_image = tkinter.Button(win, text='选择图片', command=select_file)
but_tiqu = tkinter.Button(win, text='提取文字', command=lambda: show_extracted_text(var_imagepath.get()), state=tkinter.DISABLED)  # Disable the button initially
but_cancel=tkinter.Button(win,text='清空',command=cancel)

# ----设计组件布局----
labimage.place(x=20, y=20, width=80, height=30)
laboutput.place(x=20, y=180, width=80, height=30)
entimagepath.place(x=120, y=20, width=280, height=25)
winoutput.place(x=120, y=55, width=900, height=400)
but_image.place(x=410, y=20, width=99, height=25)
but_tiqu.place(x=540, y=20, width=99, height=25)
scrollbar.place(x=1020, y=55, height=400)
but_cancel.place(x=670,y=20,width=50,height=25)

def show_extracted_text(image_path):
    if image_path:
        extracted_text = extract_ocr_text(image_path)
        winoutput.delete(1.0, tkinter.END)  # Clear the existing text in the output Text widget
        winoutput.insert(tkinter.END, extracted_text)

win.mainloop()    # 进入消息循环

二、挖坑与填坑

1、第一坑

代码中import pytesseract导入了pytesseract库,pytesseract库依赖程序tesseract.exe,但是打包后exe缺少了对tesseract.exe的依赖导致无法运行。

1、填坑

a、找到pytesseract库的pytesseract.py文件

 b、修改为下图。这样程序运行时,就通过主程序的根目录下,先找OCR文件夹,再找到tesseract.exe。

 c、打包程序的时候将Tesseract-OCR放在.py同目录下

 d、打包成功后,将Tesseract-OCR放在dist目录下

这样就解决了脚本对程序的依赖问题。

2、第二坑 

出现报错:no module named ‘pkg_resources.py2_warn

2、填坑

先用pyinstaller -D(F) xxx.py生成一下(不一定能正常运行)

经过第一步之后,目录下有个.spec文件,用记事本打开,里面有个hiddenimports,在这条里面加上pkg_resources.py2_warn

 再次用pyinstaller,注意这时候输入的命令是pyinstaller -D(F) xxx.spec

即可解决问题(已测试)

填坑方法2:(未亲自测试)

1.pip uninstaller setuptools
2.pip installer setuptools==44.0.0
(不过这种方法对setuptools进行降级处理,可能有些功能不能使用)

3、第三坑

出现报错:‘ModuleNotFoundError: No module named 'pytesseract' 

3、填坑

在该目录下找到pytesseract文件夹

 将该文件夹复制到.py主程序同目录下

然后打包即可解决该问题。

 4、第四坑

出现报错:struct.error: unpack requires a buffer of 16 bytes

填坑

我不知道怎么解决的,好像换了个目录打包就解决了,还是换了个打包命令就解决了,我也忘记是如何解决的了,

这就是遇到的所有坑,最近没休息好,没有动脑子思考这些坑产生的原因是什么,解决的原理是什么,先暂时记录下来,后期再思考补充。

三、参考文章

当主程序引用了tesseract程序,python打包出来的exe无法在其他电脑上运行的问题的解决方法_python打包不能在其他电脑打开_py617的博客-CSDN博客

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值