Python virtual environment construction, packaging into exe release and summary of problems encountered

Python virtual environment construction, packaging into exe release and summary of problems encountered

Article directory

  • Python virtual environment construction, packaging into exe release and summary of problems encountered
    • 1. Software used in the project
    • 2.Python virtual environment installation
    • 3. Problems and solutions:
      • 3.1 Use pyinstaller to compile the file to exe to generate 1. WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp\_MEI132522\torch\_C_
      • 3.2.FileNotFoundError: [Errno 2] No such file or directory: ‘C:\Users\Administrator\AppData\Local\Temp\_MEI126282\easyocr\character\ch_sim_char.txt’
      • 3.3PyInstaller compiler error:
      • 3.4pyinstaller runs too slowly after generating exe
      • 3.5 When using the image recognition library easyocr, an error message cv2.error: Unknown C++ exception from OpenCV code#
    • 4. Steps to package exe with easyocr function (if there is a better solution below, please pay attention, thank you)
      • 4.1 Execution
      • 4.2Production\dist\main
      • 4.3 Copy imageio and easyocr
      • 4.4Copy.EasyOCR/modle
      • 4.5 Package release
      • 4.6 Use
      • 4.7 Startup
      • 4.8 Activation

1. Software used in the project

python3.7

Pycharm_Professional_2021.2.1_Protable

2.Python virtual environment installation

2.1 Install pip
First download the pip-22.2.2.tar.gz source code, unzip it and cd pip-22.2.2
py.exe setup.py install

pip set source
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

2.2 Install pipenv
pip install pipenv

2.3 Activate the virtual environment in the specified directory,
Create the virtual environment directory Python_ENV on the D drive, and then cd to the directory
#Create a virtual environment pipenv install
D:\Python_ENV>pipenv install
#View the directory where the virtual environment is located
(Python_ENV-iU3StqtP) D:\Python_ENV>pipenv --where
D:\Python_ENV
#Activate virtual environment pipenv install
D:\Python_ENV>pipenv shell
Launching subshell in virtual environment...
Microsoft Windows [Version 10.0.22000.978]
(c) Microsoft Corporation. all rights reserved.


2.4 Installing the required packages for the project in the virtual environment
#qt
(bo-flwsr0G4) (base) C:\Users\bo>pip install PyQt5

(bo-flwsr0G4) (base) C:\Users\bo>pip install Pillow

(bo-flwsr0G4) (base) C:\Users\bo>pip install pycryptodome

(bo-flwsr0G4) (base) C:\Users\bo>pip install pypiwin32
(bo-flwsr0G4) (base) C:\Users\bo>pip install wmi
(bo-flwsr0G4) (base) C:\Users\bo>pip install pyyaml
(bo-flwsr0G4) (base) C:\Users\bo>pip install coloredlogs
(bo-flwsr0G4) (base) C:\Users\bo>pip install pymysql
#tool
(bo-flwsr0G4) (base) C:\Users\bo>pip install PyQt5-tools
Mouse click
(bo-flwsr0G4) (base) C:\Users\bo>pip install pymouse

#Install packaging software
There is only one key point: you must also install pyinstaller in the virtual environment
(bo-flwsr0G4) (base) C:\Users\bo>pip install pyinstaller

#install easyocr
pip install easyocr -i http://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn

If an error occurs: cv2.error: Unknown C++ exception from OpenCV code when using the image recognition library easyocr.
Reason: It is caused by the opencv version being too high and the python version being low.
Solution: Re-import opencv in the virtual environment
pip install opencv-python==4.1.2.30 -i https://pypi.tuna.tsinghua.edu.cn/simple

#View installed packages
(bo-flwsr0G4) (base) C:\Users\bo>pip list


2.5 Package exe
 Execute cmd in pycharm terminal:
  pyinstaller -D main.py --console --icon=favicon.ico --hidden-import easyocr -p

3. Problems and solutions:

3.1 Use pyinstaller to compile the file to generate exe 1. WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp_MEI132522\torch_C_

Solution: main.spec
Find the *.spec configuration file, open the file and add the following code:

for d in a.datas:
if '_C.cp37-win_amd64.pyd' in d[0]:
a.datas.remove(d)
break

The detailed contents of the main.spec configuration file are as follows:

......
a = Analysis(['main.py'],
             pathex=[],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
             
#Added code part start
for d in a.datas:
if '_C.cp37-win_amd64.pyd' in d[0]:
a.datas.remove(d)
break
#Added code part end

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
.......

3.2.FileNotFoundError: [Errno 2] No such file or directory: C:\Users\Administrator\AppData\Local\Temp\_MEI126282\easyocr\character\ch_sim_char.txt’

Solution: Add the –collect-all easyocr option after the packaging command

pyinstaller -F –onefile main.spec –collect-all easyocr –noconsole –icon=favicon.ico

3.3PyInstaller compiler error:

INFO: UPX is not available.

Reason: pyinstaller uses UPX compression,

Solution: Install UPX, and then run the package exe command (it will prompt that UPX is available)

Download UPX->Unzip the upx.exe file->Copy it to the pyinstaller directory

UPX address: https://github.com/upx/upx/releases/tag/v3.96

3.4pyinstaller runs too slowly after generating exe

When packaging pyinstaller, use -D instead of -F option.

-D: Generate a folder with multiple files in it for fast startup
-F: Only generates a file, does not expose other information, and starts slowly

3.5 When using the image recognition library easyocr, an error message is reported as cv2.error: Unknown C++ exception from OpenCV code#

Reason: It is caused by the opencv version being too high and the python version being low.

Solution: Re-import opencv in the virtual environment
pip install opencv-python==4.1.2.30 -i https://pypi.tuna.tsinghua.edu.cn/simple

4. Steps to package exe with easyocr function (if there is a better solution below, please pay attention, thank you)

4.1 Execution

 pyinstaller -D main.py --console --icon=favicon.ico --hidden-import easyocr -p C:\Users\15129\.virtualenvs\system32-zwnXhztR\Lib\site-pac
kages

4.2Production\dist\main

4.3 Copy imageio and easyocr

C:\Users\15129.virtualenvs\system32-zwnXhztR\Lib\site-packages
Replace \dist\main under imageio and easyocr

4.4 copy.EasyOCR/modle

Copy .EasyOCR under screenShot-register to main

Reason: .EasyOCR model training file, the easyocr program will read the model from the C:\Users\yourname\ directory. However, pyinstaller will not package it when packaging the exe, so you need to copy it yourself so that you can copy it to C:\Users\yourname\ for later use.

4.5 package release

Using zip job main.zip

4.6 use

Unzip main.zip

Copy .EasyOCR under main to C:\Users\yourname\

4.7 startup

Click main/mian.exe

4.8 activation

If the software has a registration function, please copy the registration code in the pop-up window to the software publisher to activate