简体   繁体   中英

cx_Freeze: Executable with pytesseract fails on other PC

I've made an executable file from my Python - OCR program ( import pytesseract ) using cx_Freeze. The resulting .exe works fine on my own machine. To test the program I made a zip-folder and shared it with some friends and colleagues on a share point. Now there is the following problem with the executable. It seems like pytesseract isn't included in the executable / zip-folder.

Here is the ErrorLog:

The file is not a PNG-file:  20181108 Kontaktanzeigen-Er_sucht_sie.jpg
The file is not a PNG-file:  20181108 Kontaktanzeigen-Sie_sucht_ihn.jpg
The file is not a PNG-file:  20181108 Language family tree.jpg
The file is not a PNG-file:  20181108 relax.jpg
The file is not a PNG-file:  20181109 Essen wie Gott am Südpol S1.JPG
The file is not a PNG-file:  20181109 Fahrzeugschein1.jpg
The file is not a PNG-file:  20181112 sie_sind_alle_so_dumm.jpg
Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 170, in run_tesseract
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py", line 707, in __init__
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py", line 992, in _execute_child
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 916, in _bootstrap_inner
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 864, in run
  File "C:\Users\TFischer\PycharmProjects\Test_Project\OCR.py", line 67, in img_to_text
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 294, in image_to_string
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 202, in run_and_get_output
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 172, in run_tesseract
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 170, in run_tesseract
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py", line 707, in __init__
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py", line 992, in _execute_child
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 916, in _bootstrap_inner
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 864, in run
  File "C:\Users\TFischer\PycharmProjects\Test_Project\OCR.py", line 73, in img_to_text1
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 294, in image_to_string
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 202, in run_and_get_output
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 172, in run_tesseract
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

The first lines are absolute clear. I've found some ideas to modify setup.py to include all imports in the executable created by cx_Freeze. Here is the latest version.

from cx_Freeze import setup, Executable
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

additional_mods = ['numpy.core._methods', 'numpy.lib.format']
additional_files = [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]

options = {
    'build_exe': {'include_files': additional_files, 'includes': additional_mods}
    }

setup(options = options,
      name = "main" ,
      version = "0.1" ,
      description = "" ,
      executables = [Executable("main.py")])

pytesseract is a wrapper for Google's Tesseract-OCR Engine. In order that pytesseract works, the Tesseract-OCR engine needs to be installed and one must be able to invoke the tesseract command as tesseract (this means that the Tesseract-OCR installation directory needs to be in the PATH).

I believe that pytesseract has been properly included in the executable but that the tesseract command does not work on the "other PC" (either Tesseract-OCR is not installed there or the Tesseract-OCR installation directory is not in the PATH there).

You need to tell the owner of the "other PC" to install Tesseract-OCR and to check that it can be started with the command tesseract from any location.

Another option would be to include the whole Tesseract-OCR installation directory in the frozen executable using the build_exe option include_files (your variable additional_files ) and to make sure that this included directory gets included in the PATH on the target PC, but this is probably not platform-independent and thus not recommended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM