当前位置:网站首页>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打包生成的可执行文件,只能在和打包器系统相同的环境下运行。也就是说,这个可执行文件不具备可移植性,若需要不同的操作系统上运行,就必须在该系统环境上重新进行打包。
边栏推荐
- The path to uniting the programmer: "titles bucket" to the highest state of pragmatic
- Jenkins自动化部署项目
- 人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准
- Study Notes - Becoming a Data Analyst in Seven Weeks "Week 2: Business": Business Analysis Metrics
- What should I do if the sql server installation fails (what should I do if the sql server cannot be installed)
- CF1320E Treeland and Viruses
- kubernate部署服务
- 六面蚂蚁金服,抗住面试官的狂轰乱炸,前来面试复盘
- 八年测试经验,为何惨遭领导痛批:你写的测试文档还不如刚来的应届生
- 开源工具推荐:高性能计算辅助工具MegPeak
猜你喜欢

权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可

内容产品进化三板斧:流量、技术、产品形态

How awesome is the "12306" architecture?

43.【list的简单属性】

高性能数据访问中间件 OBProxy(三):问题排查和服务运维

MQTT网关读取西门子PLC数据传输到阿里云平台案例教程

华为7年经验的软件测试总监,给所有想转行学软件测试的朋友几点建议

No-code development platform all application settings introductory tutorial

Why did I switch from developer to testing, 3 years software testing engineer, tell you the secret of this

Logic Vulnerability----Permission Vulnerability
随机推荐
Web消息推送之SSE
桌面软件开发框架大赏
手把手教你写让人眼前一亮的软件测试简历,收不到面试邀请算我输
BI-SQL丨WHILE
ESP32 反复重启问题 Arduino屏蔽断电探测器
LeetCode二叉树系列——102.二叉树的层序遍历
吃透Chisel语言.28.Chisel进阶之有限状态机(二)——Mealy状态机及与Moore状态机的对比
激光雷达点云语义分割论文阅读小结
开源工具推荐:高性能计算辅助工具MegPeak
Data Middle Office Construction (5): Breaking Enterprise Data Silos and Extracting Data Value
Machine learning difference in the competition and industry application
高性能数据访问中间件 OBProxy(三):问题排查和服务运维
Redis6.0 source code learning (5) ziplist
网络安全——lcx的使用
容器排序案例
LeetCode二叉树系列——144.二叉树的最小深度
The truth of the industry: I will only test those that have no future, and I panic...
VLAN实验
Application of time series database in the field of ship risk management
还在说软件测试没有中年危机?9年测试工程师惨遭淘汰