[Python Notes 02] Python uses Pyinstaller to package python source code files into exe executable files

This article mainly introduces how to use Pyinstaller to package python source code files into exe executable files.

Table of contents

1. Pyinstaller packs exe files

1.1. Installation dependencies

1.2. Execute packaging

1.3. Specify icon

1.4. Parameter introduction

1.5. Execute the program


1. Pyinstaller packs exe files

1.1. Installation dependencies

Pyinstaller is a dependency library that can package python files into exe executable files. Install dependencies through the following command:

# 按照 pyinstaller 依赖库
pip install pyinstaller

Just execute the installation command directly in the Terminal terminal in the PyCharm development tool, as follows:

1.2. Execute packaging

Here I use PyCharm as a development tool. In the Terminal terminal window, enter the following packaging command:

# 执行打包命令
Pyinstaller -F 你要打包的文件名称.py

By executing the above command, a dist directory will be generated in your workspace at this time, and the executable file packaged into exe is stored in this directory. For example, here I created a main.py program that outputs hello world, and package it into an exe executable file through Pyinstaller, as follows:

1.3. Specify icon

The exe executable file we packaged has no icon. If you want the exe file to look better, you can customize the ICO icon. First, enter the Ali icon library to download an icon you like, as follows:

After the download is complete, find an ICO online conversion website, for example: https://www.aconvert.com/cn/icon/png-to-ico/ , convert the downloaded PNG icon into an ICO icon, because of Pyinstaller Only ICO icons can be recognized.

After the production is complete, put the picture in the code directory, and then execute the following command:

# 指定图片进行打包
Pyinstaller -F -i 你的ICO图标名称.ico 你要打包的文件名称.py

After the execution is complete, you can view the exe file in the dist directory, as follows:

Open the resource manager at this time, and you can see that the exe file uses the ICO icon you just customized.

1.4. Parameter introduction

When Pyinstaller is executed, some parameters can be set. The common parameters are:

  • -F: Indicates to type the code into an exe executable file.
  • -w: Indicates that the log information will not be output in the CMD command line window. If you want to view the output information, do not use this parameter.
  • -i: Specifies the file of the ICO icon.

Case code:

# 打包单个exe文件
Pyinstaller -F xxx.py

# 打包单个exe文件,并且CMD窗口中不输出信息
Pyinstaller -F -w xxx.py

# 打包单个exe文件,并且指定ICO图标
Pyinstaller -F -i yyy.ico xxx.py

1.5. Execute the program

The packaged exe file cannot be executed directly by double-clicking, and the screen will flash by when double-clicking to execute, and there is no way to see the output log in the CMD command line window. The correct execution method is:

  • 1. First open a CMD command line window.
  • 2. Enter [xxx.exe] in the command line window (this is the exe file generated after you pack it), and press Enter to execute.
  • 3. At this point, you can see the input log information in CMD.

At this point, the introduction of Pyinstaller packaging exe files is over.

In summary, this article is over. It mainly introduces how to use Pyinstaller to package python source code files into exe executable files.

Guess you like

Origin blog.csdn.net/qq_39826207/article/details/132307724