【问题标题】:How to compile Python code with PyTesseract to EXE?如何使用 PyTesseract 将 Python 代码编译为 EXE?
【发布时间】:2021-04-05 06:39:22
【问题描述】:

我有一个使用 pytesseract 库的 Python 项目。 我在 PyCharm 中对其进行了测试。蟒蛇版。 3.7. 现在我正在尝试使用 PyInstaller 将此项目编译为 exe。

当我运行 exe 时出现错误:

ModuleNotFoundError 中的文件“getTextFromScreen.py”第 5 行:

没有名为 'pytesseract' 的模块 [9188] 无法执行脚本 main

我在代码中的导入如下所示:

import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'Tesseract-OCR\tesseract'

我在 python 项目文件夹和编译项目文件夹中提供了整个 'Tesseract-OCR' 文件夹。

我不知道我做错了什么。 我想请你帮忙

【问题讨论】:

    标签: python pyinstaller python-tesseract


    【解决方案1】:

    你在用windows吗?您必须在路径中包含 .exe 扩展名。而只是r'Tesseract-OCR\tesseract',使用r'Tesseract-OCR\tesseract.exe'。我也有一个使用 PyTesseract 的项目,在 python 项目中提供了一个完整的 tesseract 文件夹,并且在使用 PyInstaller 编译后运行良好。

    【讨论】:

    • 这解决了我的问题。谢谢!
    • 很高兴听到这个消息!
    【解决方案2】:

    如果你想创建一个 .exe 并从任何其他没有 Tesseract 的电脑上运行它,你必须使用 auto-py-to-exe 工具,在附加文件选项中附加所有 Tesseract 文件所在的文件夹已安装,然后将其放入您的代码中

    import sys
    
    if getattr(sys, 'frozen', False):
        _path = os.path.join(sys._MEIPASS, './tresseract/tesseract.exe')
        print(_path)
        pytesseract.pytesseract.tesseract_cmd =_path
        # the .exe will look here
    else:
        pytesseract.pytesseract.tesseract_cmd = r"C:\tresseract\\tesseract.exe"
        #ruta donde se encuentre su tresseract
    

    编译,祝你好运!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多