Pyinstaller packaging error summary

Question Catalog

1. Pyinstaller packages the exe file. After execution, it prompts that resource files such as yaml, csv, dll and so on are missing.
2. After packaging and running, the prompt WARNING: file already exists but should not: C:\Users\ADMINI~1\AppData\Local\Temp_MEI130922\torch_C.cp38-win_amd64.pyd.
3. Run the exe and crash.
4. (ModuleNotFoundError: No module named 'tensorflow_core.python and FileNotFoundError: No such file or directory).
5.pyimod04_ctypes.install..PyInstallerImportError: Failed to load dynlib/dll 'C:\Users\Lenovo\AppData\Local\Temp\_MEI209562\MvCameraControl.dll'. Most likely this dynlib/dll was not found when the application was frozen .

Solution

Question one:

pyinstaller -F main.py 

After packaging (I packaged it in the conda environment, it is said that the packaging effect is good. Of course, you can also use the Terminal command line in pycharm), all .py files referenced by the main.py file will be automatically packaged (py files only), and others The resource files need to be imported manually in the generated main.spec file.

a = Analysis(
    ['open_camera_main.py'],
    pathex=[],
    binaries=[],
    datas=[
    **('..\\ultralytics\\yolo\\cfg\\default.yaml', 'ultralytics\\yolo\\cfg'),
     ('C:\\Program Files (x86)\\Common Files\\MVS\Runtime\\Win64_x64','.'),
     ('..\\best.pt','.'),
    ],**
    hiddenimports=[],
    hookspath=[],
    hooksconfig={
    
    },
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

For example, the first configuration format of datas[] ('path', 'path') is the local path (absolute path can be used, it doesn't matter) and the virtual path after the exe is run. '.' is to put the resource file In the root directory, and the second data cited in this article, a folder can be referenced entirely. The third one is the trained model file.

Question 2:
There is an internal bug in the pyinstaller tool. It is said that the solution is also very simple, which is to copy the following code in the generated main.spec file. (I was prompted twice, but I didn’t understand the details, so I wrote two functions directly). To put it bluntly, it means skipping the error report.

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

Question 3:
Assign console to True in the generated main.spec file, so that you can view errors when the exe is running. (Of course, you can also directly package with -c before packaging, but you will be lazy later)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='main',
    **debug=True,** 可以debug
    bootloader_ignore_signals=False,
    strip=False,
    **upx=True,**  打包用的
    upx_exclude=[],
    runtime_tmpdir=None,
    **console=True,**   
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

Question 4:
When packaging, you will encounter many modules and packages, or FileNotFoundError: No such file or directory. A very violent method is to copy these resource files directly under the generated dist folder. To be more particular, check whether the local environment has resource files configured.

Question 5:
I placed the dll resource in data, but it showed a loading error. Then I modified the reference statement in the code and it was solved.

MvCamCtrldll = os.path.join(os.getcwd(), "MvCameraControl.dll")

Previously used:

 MvCamCtrldll = WinDLL("MvCameraControl.dll")

Tips:
1. It is said that using the conda virtual environment for packaging will have better results.
2. Also, running pyinstaller -F main.py before packaging may prompt some errors. For example, there is no **.yaml file under the a/b/c/ path. We need to package like this.

 pyinstaller -F main.py  --add-data=~/project/config/config.yaml**;**a/b/c/

Attention!!! Windows system must use semicolon

Guess you like

Origin blog.csdn.net/h1d30nbush/article/details/129744870