First of all, most of the tutorials install Pyinstaller in cmd
Use the installation command: pip install pyinstaller; use the mirror source of Tsinghua University to speed up:
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
The result is as follows:
C:\Users\little>pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pyinstaller in d:\pycharm\anaconda\lib\site-packages (5.4.1)
Requirement already satisfied: setuptools in d:\pycharm\anaconda\lib\site-packages (from pyinstaller) (58.0.4)
Requirement already satisfied: altgraph in d:\pycharm\anaconda\lib\site-packages (from pyinstaller) (0.17.2)
Requirement already satisfied: pywin32-ctypes>=0.2.0 in d:\pycharm\anaconda\lib\site-packages (from pyinstaller) (0.2.0)
Requirement already satisfied: pefile>=2022.5.30 in d:\pycharm\anaconda\lib\site-packages (from pyinstaller) (2022.5.30)
Requirement already satisfied: pyinstaller-hooks-contrib>=2021.4 in d:\pycharm\anaconda\lib\site-packages (from pyinstaller) (2022.10)
Requirement already satisfied: future in d:\pycharm\anaconda\lib\site-packages (from pefile>=2022.5.30->pyinstaller) (0.18.2)
show already installed
Second, use Pyinstaller to package your project into exe
Single file packaging
pyinstaller -F -w -i xxx.ico main.py --noconsole
Pack multiple files
pyinstaller [主文件] -p [其他文件1] -p [其他文件2] --hidden-import [自建模块1] --hidden-import [自建模块2]
Common parameter meanings
-i or -icon generate icon
-F create a bundled executable
-w use window, no console
-C use console, no window
-D create a single folder package containing the executable ( by default)
-n filename
For example my project:
correction.py | main running program |
add_dialog.py | The dialog interface program that jumps from the main interface |
cubic_spline.py | A caller of the main program |
correction.ui | Main interface |
add_dialog.ui | dialog interface |
pyinstaller -F -w correction.py -p add_dialog.py -p cubic_spline.py --hidden-import correction --hidden-import add_dialog --hidden-import cubic_spline
But the exe generated in this way will crash and cannot run due to lack of import
my method
1. In the terminal in pycharm, pip install pyinstaller
2. In the terminal in pycharm, pyinstaller -F -w correction.py -p add_dialog.py -p cubic_spline.py --hidden-import correction --hidden-import add_dialog --hidden-import cubic_spline
After successfully, two folders xxx.spec files, build and dist, will be generated under the folder of the project
3. Open the newly generated dist folder under the folder where the project is located. Just run the exe file inside.
PS: Since my background and icons are added in the main interface program under correction.py, I do not use --icon in pyinstaller
# 设置背景图片
MainWindow.setStyleSheet('#MainWindow{border-image:url(background.jpg);}')
# 设置图标
MainWindow.setWindowIcon(QtGui.QIcon('logo.ico'))
When running the application, if there is no background and icon display, you need to put your background image and icon into the dist folder, and the background image and icon will be displayed when running