PyInstaller tutorial and using PyInstaller to package PySide6 program

1. Installation and use

Use the following command to install

pip install pyinstaller

Basic commands:

# Syntax: pyinstaller option source file
pyinstaller -F main.py

Common options:

optioneffect
-F,-onefileGenerate a single executable
-D,--onedirGenerate a directory (containing multiple files) as an executable program
-a,--asciiDoes not contain Unicode character set support
-d,--debugGenerate debug version executable
-w,--windowed,--noconsoleSpecifies that the command line window is not displayed when the program runs (valid for Windows only)
-c,--nowindowed,--consoleSpecifies to run the program using a command line window (valid for Windows only)
-o DIR,--out=DIRSpecifies the build directory for the spec file. If not specified, the current directory is used by default to generate spec files
-p DIR,--path=DIRSet the path of the Python import module (similar to setting the PYTHONPATH environment variable). You can also use path separators (semicolons for Windows and colons for Linux) to separate multiple paths
-n NAME,--name=NAMESpecifies the name of the project (resulting spec). If you omit this option, the main file name of the first script will be the name of the spec
-iAdd Icon
--hidden-importImport modules that are not packaged
--pathsSpecifies the path to the import file

You can view detailed option information through the pyinstaller -h command.
And give the official instructions

2. Package the PySide program with PyInstaller

First, use the following command to package:

pyinstaller -F -w main.py


After running the packaged exe file, the above error is reported because the PySide6.QtSvg module is not imported. For this error, directly use the - \ - hidden Import command to import the missing module during packaging.

The command is as follows:

pyinstaller -F -w main.py --hidden-import PySide6.QtSvg

At the same time, it is noted that the following warnings exist during packaging:

4777 WARNING: lib not found: shiboken6.abi3.dll dependency of D:\ProgramData\Anaconda3\envs\pyside6\lib\site-packages\PySide6\QtSvgWidgets.pyd
4793 WARNING: lib not found: shiboken6.abi3.dll dependency of D:\ProgramData\Anaconda3\envs\pyside6\lib\site-packages\PySide6\QtNetwork.pyd
4815 WARNING: lib not found: shiboken6.abi3.dll dependency of D:\ProgramData\Anaconda3\envs\pyside6\lib\site-packages\PySide6\QtWidgets.pyd
4831 WARNING: lib not found: shiboken6.abi3.dll dependency of D:\ProgramData\Anaconda3\envs\pyside6\lib\site-packages\PySide6\QtGui.pyd
4845 WARNING: lib not found: shiboken6.abi3.dll dependency of D:\ProgramData\Anaconda3\envs\pyside6\lib\site-packages\PySide6\QtCore.pyd
5028 WARNING: lib not found: shiboken6.abi3.dll dependency of D:\ProgramData\Anaconda3\envs\pyside6\lib\site-packages\PySide6\pyside6.abi3.dll

The reason for the warning is that the dynamic link library was not found: shiboken6.abi3.dll
This file can generally be found in the local PySide6 library. Search shiboken6.abi3.dll locally to find the path of the file;
If not, you can download the file at this link: Download link

The solution seen on the Internet is to add the path of the directory where shiboken6.abi3.dll file is located to -- path, that is:

pyinstaller -F -w main.py --hidden-import PySide6.QtSvg --paths=D:\ProgramData\Anaconda3\envs\pyside6\Lib\site-packages\shiboken6

Error is still reported after running exe file:

Troubleshooting:
1) According to the error message, it means that WindowsPath cannot be added to the string, so it has always been thought that Python does not support a syntax of PyInstaller or PySide6, so PyInstaller and PySide6 are deleted and installed, which has no effect;
2) It is useless to replace conda with pip, reinstall PyInstaller and PySide6, or replace pip as the default source.

Finally, enter the location where the error occurred: PySide6\__ init __.py

Find where the error occurred, line 26

As you can see from this code, you actually want to tell me that shiboken6 is not found, but there may be some syntax errors in the composition of the prompt message, which mislead us. The fundamental reason is that shiboken6 is not found

So add shiboken6.abi3.dll to the path, that is:

pyinstaller -F main.py --hidden-import PySide6.QtSvg --paths D:\ProgramData\Anaconda3\envs\pyside6\Lib\site-packages\shiboken6\shiboken6.abi3.dll

The package is successful. There is no problem running exe.

Alternatively, you can directly copy shiboken6.abi3.dll to the project path, that is:

pyinstaller -F main.py --hidden-import PySide6.QtSvg --paths shiboken6.abi3.dll

3. Small tip

If the packaged application is a form program, you need to use the - w option in the final command to indicate that the command line window is not displayed.
However, if you are not sure whether the current package lacks files or modules, you can not add - w first, so that the resulting errors can be output in the command line window for debugging.

After the program is packaged, an EXE file is generally generated. If you double-click exe, the command line window will flash back, and you can open the EXE file in another way.

First open the command line (cmd)
Then drag the exe file to the command line

Click enter to run the exe file. At this time, the command line window will not exit. You can view the error information in the window for debugging.

Problem: sometimes there are problems. For example, if the project uses the relative path to find the settings.json file, but the working space of cmd is C:\Users\Administrator, it will go to C:\Users\Administrator\settings.json to find it. If it cannot find it, an error will be reported

Solution: during debugging, open cmd in the directory where exe is located, or switch cmd workspace to the directory where exe is located, and then drag the EXE file to run

After debugging to ensure that the exe file runs correctly, that is, after determining the options to be added for the packaging command, add the - w option to generate an exe program without a command line window.

Finally, remember to copy some resources (such as json files, pictures, folders required by some projects, icons, xml, etc.) to the packaged exe file directory, or to the relative directory location previously written in the code.

4. Reference

http://c.biancheng.net/view/2690.html

Keywords: Python Anaconda pip pyqt pyinstaller

Added by shibbi3 on Fri, 19 Nov 2021 14:54:46 +0200