Desktop application packaging: pyinstaller | Jingdong logistics technical team

1 background

When using python to develop some small tools, if other people's computers do not have a python environment or have not installed the corresponding third-party library, there is no way to run it, and it is unrealistic to ask the other party to install it, especially if the other party is not a technician, so how to use it What about an independent python program that makes it a desktop application that can be run by double-clicking regardless of the environment? Packaging with pyinstaller is a good choice.

2 What is pyinstaller

pyinstaller is a third-party library that supports cross-platform use. It can automatically analyze, collect and generate a folder or executable file for the modules and libraries required for script execution.

The following example uses the windows environment. Due to the cross-platform nature of pyinstaller, it is the same as the Unix-like environment command, but the final executable file is different.

3 How to install

Installing the pyinstaller module is the same as installing other python modules, just use the pip command to install it.
pip install pyinstaller
installation output:

Among them, altgraph, future, etc. represent the PyInstaller module dependent environment.

4 How to use

4.1 Introduction to common commands

pyinstaller + options + python entry source file

The main options include:

  • -F is packaged into one exe file (if there are multiple py files, there is a solution below)
  • -D automatically create a directory containing desktop application files, including all runtime dependencies (default option)
  • -w Specifies that the command line window is not displayed when the program is running (only valid for windows)
  • -i + ico path can replace the application icon (if it is in the current folder, no need to add the ico path)
  • -d Generate a debug version of the exe file
  • -o specifies the .spec file generation directory, if not specified, it will be output in the current root directory
  • -n specifies the .spec file name, if omitted, the main file name will be used as the .spec file name
  • -p Set the import path, you can use the path separator (windows is a semicolon, unix-like is a colon)

4.2 Examples of common methods

4.2.1 Packaging into a single file

Using the following command will make the python script packaged into an exe file.
pyinstaller -F -w -i "./dian_128.ico" start_dian.py

in:

  • -F commands are packaged into a single file
  • -w no command line window
  • -i specifies the exe file icon icon

After successful execution, two folders will be generated in the root directory. The "build" file contains all dependent files, and the "dist" folder stores the generated exe file.

4.2.2 Packaged into folders

Using the following command will make the python script packaged into a folder.
pyinstaller -D -w -i "./dian_128.ico" start_dian.py

in:

  • The -D command packs into a folder
  • -w no command line window
  • -i specifies the exe file icon icon

After the command line is successfully executed, a start_dian subdirectory will be generated in the dist directory, which contains a large number of .dll files and .pyz files, which are all support files for the exe file.

4.3 Use .spec file to configure compilation

The above explanations are all general versions, which are not very flexible. If you want to add new resources, or do not want to enter a long command every time you compile, you can use the .spec file to perform customized configuration and compilation.

4.3.1 Generate .spec file

After compiling using the above method, if the .spec generation directory is not specified, a .spec file with the same name as the compiled py file will be generated in the current root directory; or use the command
pyi-makespec -F start_dian.py, it will be generated directly. spec file;

4.3.2 Contents of the spec file

open spec file

Only a few key variables involved are explained here:

1.Analysis:

  • ['start_dian.py']: It is the main file and all dependent files, if the dependent files are all in the same directory, it is not necessary to write;
  • oathex: the absolute path of the project;
  • datas: Add resource files, such as folders, pictures, excel, etc., with tuples as parameters, the first parameter is the original path, and the second parameter is the compiled target path;

2.EXE:

  • console: Whether to open the command line, the default is True (open);
  • icon: Compile the icon of the exe executable file product, and must use an absolute path;
  • name: The name in EXE and COLLECT is the name of the compiled file, which is the same name as the compiled main file by default;

4.3.3 spec file compilation

Configured spec file, execute the command to compile, no other parameters are needed:
pyinstaller start_dian.spec

4.3.4 In the following four situations, it is better to modify the spec file:

  1. When you need to bundle resource files with exe files;
  2. When your dependent files include .dll or .so files;
  3. When you run exe you need run parameters;
  4. When you need to merge multiple packages into a common module;

5 Frequently Asked Questions

1. How to package multiple files

  • Add the imported module name in the Analysis parameter hiddenimports in the .spec file. After modification, it can be packaged again, which will solve the problem that the dependent package of the exe file cannot be found;
  • Use -p main file + -p import file... in the command to directly generate the exe file.

2.exe file is too large

  • Install python virtual environment;
  • When importing a package, use form + package name + import + function, do not directly import + package name.

3. There is an inexplicable error

  • The full path where the python file is located, try not to have Chinese.

4. After packaged into a folder, it cannot be used by others

  • The folder under dist needs to be sent as a whole, keeping the path of the exe file and the folder under dist unchanged.

5. After running the exe file, the expected effect is not achieved

  • The file reports an error, repackage the file, use the console (remove the -w parameter), and the program running process and printing errors will be displayed on the console.

6. The python script is mainly command line output, but after the program is executed, it exits and cannot view the output information

  • Add commands to the last line of the python script: os.system("pause") or raw_input("output any key to exit").

Author: JD Logistics Luo Tonglei 

Source: JD Cloud Developer Community Ziyuanqishuo Tech

Huawei officially released HarmonyOS 4 miniblink version 108 successfully compiled. Bram Moolenaar, the father of Vim, the world's smallest Chromium kernel, passed away due to illness . ChromeOS split the browser and operating system into an independent Bilibili (Bilibili) station and collapsed again . HarmonyOS NEXT: use full The self-developed kernel Nim v2.0 is officially released, and the imperative programming language Visual Studio Code 1.81 is released. The monthly production capacity of Raspberry Pi has reached 1 million units. Babitang launched the first retro mechanical keyboard
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10093734