Introduction to using Pyinstaller and several problems encountered

Pyinstaller is a tool for packaging python programs. pyinstaller itself can be used cross-platform, but the cross-platform here refers to pyinstaller itself. The files packaged by pyinstaller cannot be cross-platform. Use pyinstaller to package python programs under MacOS. After packaging, The content can only be run under MacOS, and the python program packaged under Windows can only be run under Windows.

Use of Pyinstaller

The command syntax of the PyInstaller tool is as follows:

pyinstaller options Python source files

The relevant parameters are explained as follows:

The two most important parameters of pyinstaller are the -F and -D parameters.

Using the -F parameter, pyinstaller will package the python script into a single exe file

Use the -F parameter. When the program generation is completed, you will see an additional dist directory in the app directory and an app.exe file in the directory. This is the EXE program generated using the PyInstaller tool.

Using the -D parameter, pyinstaller will package the python script into a folder. When running the program, you need to enter the folder and click to run the corresponding executable program (default option)

Using the -D parameter, you will see an additional dist directory under the app directory, and an app subdirectory under this directory. This subdirectory contains a large number of .dll files and .pyz files, all of which It is the support file of the app.exe program.

There are also the following parameters and meanings:

-h,--help View the help information for this module
-F,-onefile Produce a single executable file
-D,--onedir Produce a directory (containing multiple files) as an executable program
-a,--ascii Does not include Unicode character set support
-d,--debug Generate a debug version of the executable file
-w,--windowed,--noconsolc Specifies not to display the command line window when the program is running (valid for Windows only)
-c,--nowindowed,--console Specifies to use the command line window to run the program (valid for Windows only)
-o YOU,--out=YOUR Specify the directory where the spec file is generated. If not specified, the current directory is used by default to generate the spec file.
-p DIR,--path=DIR Set the path to Python imported modules (similar to setting the PYTHONPATH environment variable). You can also use a path separator (semicolon on Windows, colon on Linux) to separate multiple paths
-n NAME,--name=NAME Specify the project (generated spec) name. If this option is omitted, the name of the first script's main file will be used as the name of the spec.

Here we need to focus on -w. After using this parameter, a black window will not pop up when running the .exe file.

In addition, when we need to add our own icon to the upper left corner of the software, we need to use the following statement:

pyinstall -i xxx.ico -n xxx -w -D entrance.py

The above is basically the basic method of packaging python programs using Pyinstaller.

Let’s talk about some of the issues you need. First, if you use the above statement to package the project and find that the icon of xxx.ico cannot be displayed on the running program, you can try to place the image file in the same directory as entry.exe and run it. .

In addition, if you find the .pyc file when using parameter -D for packaging, you can find it in the _pycache_ folder of the corresponding original project folder and put it into a new directory at the same level as entrace.exe, and run it again. Can.

Guess you like

Origin blog.csdn.net/zhaodongdz/article/details/122523420