In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, Xiaobian will bring you about how to use pyinstaller to package python PyQt5 programs. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
When you have the need to package your python application, you may seek help from search engines. You may have searched for words such as "python to exe","python packaging", etc. You may have seen various related solutions. Now I am introducing one of them, but it can be responsibly said that this is the best solution at present, and it is the best solution across platforms. That is to say, using this tool called pyinstaller, You can package the same code to run on Windows, Linux, and macOs.
The name of this tool is pyinstaller, official homepage:
http://www.pyinstaller.org/
This tool encapsulates packaging work into actions that can be done with a single command.
Prepare tools. Prepare.
The first thing of course is to install the library, as usual, using pip
pip install pyinstaller source code preparation
Since we are packaging for a GUI application, we need to make a simple modification to the source code, which is not necessary if you are packaging the command line.
First of all, we want to create an entry file named wifidrop.pyw. The name doesn't matter, it will become your final software name. What needs to be noted here is that our file suffix is pyw, which is one more w than the py file suffix, and w means window. That is to say, tell the python environment that our software is a window application. If we don't do this, when running the software, in addition to our GUI interface, python will leave a command line window behind. This is obviously not what we want, and with the pyw suffix python doesn't show command line windows.
There isn't much to do in wifidrop.pyw, just a simple call to the main function in main.py.
import sysfrom main import mainif __name__ == '__main__': excode = main() sys.exit(excode) package
After preparing the code, you can use pyinstaller to package the software. The process is also very simple. One line of command can solve it. Run it on the command line.
pyinstaller --clean -F -w wifidrop.pyw -i Treetog-I-Documents.ico Parameters
-clean: tells pyinstaller to delete cache and temporary files
-F: Tell pyinstaller to put the packaged result in an exe file, that is to say, the final result will only be an exe file. If this parameter is not used, the result will be an exe plus many dependent files, which is not conducive to our distribution of software.
-w: Tell pyinstaller what we want to generate is a window application
-i: Specify an icon for our app, otherwise the python icon will be used by default
In general, these parameters are sufficient for daily use, but if you need more in-depth features, such as encryption, you need to read the manual.
After this command runs successfully, you will see the build and dist folders in the project directory, and the final executable files will be placed in the dist folder.
Improvements for PyQt applications Current minor issues
When we try to run the wifidrop.exe file generated in dist, you will find that the program does not run, I do not know if you have any impression, so far, we have been using the loadUi function in main.py to load the ui created by Qt Creator to create GUI interface, so carefully observe the dist folder, there is no ui file, right?
Because pyinstaller is just a packager for py applications, it doesn't know about the other files, so you can try copying our two ui files into the dist folder before running this exe.
You will notice that the app is now working properly. But is there any risk?
To put it bluntly, the ui file is a text file in xml format. If the user of our software intentionally or unintentionally modifies the contents of these two files, our software is very likely not to run. Therefore, when distributing our software, we should avoid using ui files.
The advantage of ui files is that they can be quickly modified through Qt Creator during development.
solutions
Of course PyQt also provides a good solution, it provides a small script that can help us convert ui files into py files and UI into classes. We convert mainwindow and dialog to python classes by running these two commands:
python -m PyQt5.uic.pyuic -x dialog.ui -o dialog.pypython -m PyQt5.uic.pyuic -x mainwindow.ui -o mainwindow.py
Each ui file corresponds to a py file, and the py file has a corresponding class.
With py module, we also need to change the way of loading ui files to instantiate python classes to load ui in the place where ui is instantiated (that is, main.py). We need to modify the initialization functions of MainWindow and SendDialog respectively. Select 2 option to load ui.
class MainWindow(QMainWindow): """Main window""" def __init__(self): super(MainWindow, self).__ init__() # UI setup - 1 option # dynamic load ui for development purpose # self.ui = loadUi('./ mainwindow.ui', self) # Use py to setup UI - 2 option self.ui = Ui_MainWindow() self.ui.setupUi(self) self.setStatusBar(None) # https://doc.qt.io/qt-5/qmainwindow.html#setStatusBaclass SendDialog(QDialog): def __init__(self, url_list, socket_server_thread, device_discover_thread, socket_broadcast): super(SendDialog, self).__ init__() # UI setup - 1 option # dynamic load ui for development purpose # self.ui = loadUi('./ dialog.ui', self) # Use py to setup UI - 2 option self.ui = Ui_dialog() self.ui.setupUi(self) Package again
After the code is fixed, let's use the same pyinstaller command above to package it. After it runs successfully, click on the generated wifidrop.exe and you will find that the software can run well without ui files.
Code Repository Description
The packaging command and ui to py command used in this article can be found under tutorial-6-direct-dist and tutorial-6-dist tag in github repository www.example.com. The difference between these two tags is that the former uses the UI file loading method to display UI, and the latter corrects this problem and uses class instantiation to display UI. https://github.com/pythonlibrary/wifidrop
The above is how to use pyinstaller to package Python PyQt5 program, if there is a similar doubt, may wish to refer to the above analysis to understand. If you want to know more about it, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.