当前位置:网站首页>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打包生成的可执行文件,只能在和打包器系统相同的环境下运行。也就是说,这个可执行文件不具备可移植性,若需要不同的操作系统上运行,就必须在该系统环境上重新进行打包。
边栏推荐
- 05 | login background: based on the password login mode (below)
- Conversion between pytorch and keras (the code takes LeNet-5 as an example)
- 机器学习在竞赛和工业界应用区别
- 数据中台建设(五):打破企业数据孤岛和提取数据价值
- 六面蚂蚁金服,抗住面试官的狂轰乱炸,前来面试复盘
- HCIP(第十五天) —— 交换机(一)
- 【VMware虚拟机安装mysql5.7教程】
- 我为何从开发人员转做测试,3年软件测试工程师,带你聊聊这其中的秘辛
- [VMware virtual machine installation mysql5.7 tutorial]
- 3 years of software testing experience, the interview requires a monthly salary of 22K, obviously he has memorized a lot of interview questions...
猜你喜欢
随机推荐
权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可
CF338E Optimize!
Synology system installation related file sharing
深入浅出零钱兑换问题——背包问题的套壳
Jenkins自动化部署项目
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
mongodb打破原则引入SQL,它到底想要干啥?
手把手教你写让人眼前一亮的软件测试简历,收不到面试邀请算我输
电池包托盘有进水风险,存在安全隐患,紧急召回52928辆唐DM
20220729 Securities, Finance
CF1320E Treeland and Viruses
记面试外包公司的一次经历,到底该不该去?
ECCV 2022 | 通往数据高效的Transformer目标检测器
激光雷达点云语义分割论文阅读小结
无代码开发平台全部应用设置入门教程
【Advanced Mathematics】【7】Double Integral
吃透Chisel语言.29.Chisel进阶之通信状态机(一)——通信状态机:以闪光灯为例
The path to uniting the programmer: "titles bucket" to the highest state of pragmatic
jsArray array copy method performance test 2207300823









