当前位置:网站首页>PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件
PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件
2022-07-30 13:56:00 【Ding Jiaxiong】
PyQt5快速开发与实战
9. 第9章 PyQt5 扩展应用
9.1 使用PyInstaller打包项目生成exe文件
PyInstaller是一个很好用而且免费的打包工具,支持 Windows、Linux、Mac OS,并且支持32位和64位系统。
安装环境
安装Pyinstaller
pip install Pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
Pyinstaller的使用
pyinstaller [opts] xxx.py
可选的参数有:
- -F,-onefile,打包成一个 EXE文件。
- -D,-onedir,创建一个目录,包含EXE文件,但会依赖很多文件(默认选项)。
- -c,-console,-nowindowed,使用控制台,无窗口(默认).
- -w, -windowed, -noconsole,使用窗口,无控制台。
打包测试
colorDialog.py
# -*- coding: utf-8 -*- from PyQt5.QtWidgets import QApplication, QPushButton, QColorDialog , QWidget from PyQt5.QtCore import Qt from PyQt5.QtGui import QColor import sys class ColorDialog ( QWidget): def __init__(self ): super().__init__() color = QColor(0, 0, 0) self.setGeometry(300, 300, 350, 280) self.setWindowTitle('颜色选择') self.button = QPushButton('Dialog', self) self.button.setFocusPolicy(Qt.NoFocus) self.button.move(20, 20) self.button.clicked.connect(self.showDialog) self.setFocus() self.widget = QWidget(self) self.widget.setStyleSheet('QWidget{background-color:%s} '%color.name()) self.widget.setGeometry(130, 22, 100, 100) def showDialog(self): col = QColorDialog.getColor() if col.isValid(): self.widget.setStyleSheet('QWidget {background-color:%s}'%col.name()) if __name__ == "__main__": from pyqt5_plugins.examples.exampleqmlitem import QtCore QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) app = QApplication(sys.argv) qb = ColorDialog() qb.show() sys.exit(app.exec_())
执行命令
pyinstaller -F -w colorDialog.py
运行测试
运行时出现了这个错误。
像是说什么东西没有找到,开始谷歌…
from PyInstaller.utils.hooks import copy_metadata datas = copy_metadata('pyqt5_plugins') + copy_metadata('qt5_tools') + copy_metadata('qt5_applications')
在路径中加上了这个文件,打包命名改成
pyinstaller -F -w colorDialog.py --additional-hooks-dir=.
中途还报了很多类似于没有模块的错误,我都一个一个的在主文件中import了
最后
两种方式运行对比。一样的
Pyinstaller原理简介
PyInstaller其实就是把 Python解释器和脚本打包成一个可执行文件,和编译成真正的机器码完全是两回事。所以千万不要指望打包成可执行文件会提高运行效率,相反,可能会降低运行效率,但是这样做的好处是在运行者的机器上不用安装 Python和脚本所依赖的库。在 Linux操作系统下,它主要使用的是 binutil工具包中的ldd和objdump命令。
输入指定的脚本后,首先 PyInstaller会分析该脚本所依赖的其他依赖,然后进行查找,并复制,把所有相关的依赖都收集起来并进行加密处理,包括Python 解释器,最后把这些文件放在一个目录下,或者打包到一个可执行文件中。然后就可以直接运行所生成的可执行文件,不需要安装其他包或某个版本的Python。需要注意的是,使用 PyInstaller打包生成的可执行文件,只能在和打包器系统相同的环境下运行。也就是说,这个可执行文件不具备可移植性,若需要不同的操作系统上运行,就必须在该系统环境上重新进行打包。
边栏推荐
- Cookie simulation login "recommended collection"
- AT4108 [ARC094D] Normalization
- 【Pytorch】如何在关闭batch-norm的同时保持Dropout的开启
- Hello,World
- BI-SQL丨WHILE
- 人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准
- 【ROS进阶篇】第十一讲 基于Gazebo和Rviz的机器人联合仿真(运动控制与传感器)
- 接口自动化框架,lm-easytest内测版发布,赶紧用起来~
- 00 testers of seasoning after nearly a year, whether to change careers or to learn the software testing students summarized the following heart advice
- 跳槽前,把自己弄成卷王
猜你喜欢
桌面软件开发框架大赏
(HR Interview) Most Common Interview Questions and Skilled Answers
自动化测试之数据驱动DDT详细篇
记面试外包公司的一次经历,到底该不该去?
网络安全——lcx的使用
The path to uniting the programmer: "titles bucket" to the highest state of pragmatic
Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review
A new generation of open source free terminal tools, so cool
经典测试面试题集—逻辑推理题
Flask Framework - Flask-Mail Mail
随机推荐
Digital signal processing course lab report (what foundation is needed for digital signal processing)
Eclipse connects to SQL server database "recommended collection"
Baijiahao cancels the function of posting documents on the interface: the weight of the plug-in chain is blocked
selenium4+pyetsst+allure+pom进行自动化测试框架的最新设计
The website adds a live 2d kanban girl that can dress up and interact
跳槽前,把自己弄成卷王
00后测试员摸爬滚打近一年,为是否要转行或去学软件测试的学弟们总结出了以下走心建议
[ARC092B] Two Sequences
Simple understanding of Precision, Recall, Accuracy, TP, TN, FP, FN
获取Google Advertising ID作为唯一识别码
内容产品进化三板斧:流量、技术、产品形态
数字信号处理课程实验报告(数字信号处理需要什么基础)
Flask框架——Sijax
AT4108 [ARC094D] Normalization
pytorch学习记录(六):循环神经网络 RNN & LSTM
Flask Framework - Sijax
sql中ddl和dml(sql与access的区别)
手把手教你写让人眼前一亮的软件测试简历,收不到面试邀请算我输
OFDM Sixteen Lectures 3- OFDM Waveforms
sql server安装失败怎么办(sql server安装不了怎么办)