python3GUI--PyQt5 packaging experience (detailed graphic demonstration)


one. Preface

A friend sent me a private message asking me how to package the project and how to reduce the size of the packaged file. The author has participated in the creation of many PyQt5 projects, and has some experience in packaging projects into executable exe programs. This article will introduce in detail Pyinstaller packaging PyQt5 projects. After reading this article, you will get: 1. How to correctly use packaging tools to package PyQt5 projects elegantly 2. How to reduce the file size of PyQt5 projects after packaging. Let's go!

two. Preparation & Introduction

1.Preparation

1.pyinstaller
2.pipenv virtual environment

2. Introduction

1.pyinstaller

PyInstaller freezes (packages) Python applications into standalone executable files. It can build smaller executables, is fully multi-platform, and uses OS support to load dynamic libraries, ensuring full compatibility.

Install

pip install pyinstaller

The version I use is: 4.7

2.pipenv

pipenv is a Python package management tool. It is the work of Kenneth Reitz, the author of requests, and provides management between various versions of Python and various package management. When you start to get familiar with and use it, you will know that it is somewhat similar to the taste of npm and yarn on the front end.
It automatically creates and manages virtual environments for projects. When you use pipenv, it will create a Pipfile in the project root directory to record the version information of the package. When you use pipenv to install a package, it will generate a Pipfile.lock file in the project root directory to lock the version and dependency information of the installation package to avoid build errors.
Install

pip install pipenv

The version used by the author is: 2022.1.8

three. Project packaging

1.Preparation

Resources
Here I take one of my PyQt5 projects as an example to demonstrate packaging PyQt5 into a file. The project structure is as follows:
Insert image description here
Briefly introduce the project:

1.CWidgets.py custom component
2.engine.py software core engine file, which can be run independently of the project and does not rely on GUI
3.qr_code_ui.py main interface GUI, converted from qr_code_ui.ui using ui tools
4. qr_GUI.py is the software main interface scheduling, which is also the entrance to this GUI. The above files are scheduled in it.
5. resource_rc.py The resource index file is converted from resource_rc.qrc using the rcc tool.

This time, the above five files are packaged into one file.

All icon files are placed in the icons folder, and qrc is used to generate the index of these icon files. The file structure is as follows: Use the
Insert image description here
rcc tool to convert the .qrc file into a .py file, namely resource_rc.py, for project reference. Write this wherever you need to use resource files:

Insert image description here

2. Packing

1. Packaging parameters

Insert image description here

2.Virtual environment

Start the virtual environment before packaging.
Open cmd in the project root directory and start the pipenv virtual environment.

pipenv shell

Insert image description here

This time, the project is packaged into a file and added to the icon ico.ico,
so the command is:

pyinstaller -F -w -i ico.ico qr_GUI.py --hidden-import  CWidgets.py --hidden-import engine.py  --hidden-import  qr_code_ui.py --hidden-import resource_rc.py

Insert image description here

After packaging is completed, there will be several more folders in the project root directory, among which the dist folder is the exe folder. The size of this virtual environment packaged file is 54.1M
Insert image description here

three. Summarize

This time we demonstrate how to use pyinstaller to package PyQt5 projects in a virtual environment.

Guess you like

Origin blog.csdn.net/a1397852386/article/details/132513170