Pyinstaller 打包EXE(七) 百篇文章学PyQT

您所在的位置:网站首页 pyqt打包deb Pyinstaller 打包EXE(七) 百篇文章学PyQT

Pyinstaller 打包EXE(七) 百篇文章学PyQT

2024-02-01 12:50| 来源: 网络整理| 查看: 265

        本文章是百篇文章学PyQT6的第七篇,本文讲述如何使用Pyinstaller打包UI界面和代码,将程序打包成EXE来更为方便的进行部署,在写博客和学习的过程中会遇到很多问题,例如:PyQT6在网上很多博客都是PyQT5、或者PyQT4大部分都和PyQT6不一样,因为PyQT6比较新所以相关博客文章会比较少,博主在本篇文章中将遇到和踩过的坑总结出来,可以供大家参考,希望大家安装顺利。包括 安装、调试、遇到问题的解决方案、怎么卸载全部解决方案。

        完全解决:qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: minimal, offscreen, windows, direct2d. 问题

        使用环境:QT6、PyQT6、PySide6 打包工具Pyinstaller

  本文作者原创,未经允许禁止转载。

Pyinstaller 打包EXE(七) 百篇文章学PyQT目录

1 安装Pyinstaller

1.1 安装不成功

2 打包

2.1 直接打包

2.2 所有文件打包

2.3 依赖打包

2.4 spec 文件

2.5 运行示例图

3 程序异常

3.1 系统变量

3.2 使用临时环境

4 完整代码

5 系列文章

1 安装Pyinstaller pip install Pyinstaller

也可以在设置里面搜索Pyinstaller

1.1 安装不成功

解决办法是,先用pip安装命令安装pywin32和wheel模块,再安装Pyinstaller模块:

pip install pywin32 pip install wheel

      安装完Pyinstaller后,就可以使用它对.py文件进行打包了。

2 打包

        语法:

pyinstaller --paths PyQt5模块路径 -F -w --icon=窗口图标文件 文件名.py # 参数说明: # --paths:指定第三方模块的安装路径。 # -w:表示窗口程序。 # --icon:可选项,如果设置了窗口图标,则指定相应文件路径;如果没有,则省略。 # 文件名.py:窗口程序的入口文件。

      普通Python程序指的是完全使用Python内置模块或者对象实现的程序,程序中不包括任何第三方模块。使用Pyinstaller的打包步骤如下。       在需要打包的.py文件所在路径目录打开终端,输入打包命令进行打包。

        软件目录:

2.1 直接打包

        将程序打包成EXE,依赖文件会在目录中。

pyinstaller 文件名.py

 

2.2 所有文件打包

        只生成一个exe,相关依赖文件都打包进EXE之中。

pyinstaller -F 文件名.py

2.3 依赖打包

        命令说明:

pyinstaller -F -w 文件名.py pyinstaller --paths PyQt5的bin目录路径 -F -w 文件名.py

        示例:

pyinstaller --paths D:\Python311\Lib\site-packages\PySide6\plugins\platforms -F -w main.py 2.4 spec 文件

        在执行完 pyinstaller -F  命令后如果需要添加依赖文件则不需要重新生成也可以,直接修改 .spec文件 然后运行.spec重新生成就可以。

2.5 运行示例图

3 程序异常

qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "D:\Python311\Lib\site-packages\PySide6\plugins\platforms" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: minimal, offscreen, windows, direct2d.

3.1 系统变量

        系统变量中,添加 QT_PLUGIN_PATH 一项,值为D:\Python311\Lib\site-packages\PySide6\plugins\platforms

        作者使用的是系统变量 设置完系统变量之后需要重启计算机 示例:

import os dirname = os.path.dirname(PySide6.__file__) plugin_path = os.path.join(dirname, 'plugins', 'platforms') os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

3.2 使用临时环境 import os os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'D:\venvforqt\Lib\site-packages\PyQt5\Qt5\plugins\platforms' 4 完整代码 import sys import PySide6 from PyQt6.uic.uiparser import QtCore from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton, QLabel, QWidget, QHBoxLayout from PySide6.QtCore import QFile, SIGNAL from Sgnals import Ui_Dialog from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale, QMetaObject, QObject, QPoint, QRect, QSize, QTime, QUrl, Qt) from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QGradient, QIcon, QImage, QKeySequence, QLinearGradient, QPainter, QPalette, QPixmap, QRadialGradient, QTransform) from PySide6.QtWidgets import (QApplication, QDialog, QPushButton, QSizePolicy, QWidget) import os dirname = os.path.dirname(PySide6.__file__) plugin_path = os.path.join(dirname, 'plugins', 'platforms') os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path # envpath = r'D:\Qt6\6.4.0\msvc2019_64\plugins' # os.environ["QT_PLUGIN_PATH"] = envpath class Ui_Dialog(object): def setupUi(self, Dialog): if not Dialog.objectName(): Dialog.setObjectName(u"Dialog") Dialog.resize(345, 221) self.pushButton = QPushButton(Dialog) self.pushButton.setObjectName(u"pushButton") self.pushButton.setGeometry(QRect(140, 90, 111, 41)) self.retranslateUi(Dialog) QMetaObject.connectSlotsByName(Dialog) # setupUi def retranslateUi(self, Dialog): Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None)) self.pushButton.setText(QCoreApplication.translate("Dialog", u"\u53d1\u9001\u4fe1\u53f7", None)) # retranslateUi class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) def show_msg(): print("btn clicked..") dialog = QMessageBox(parent=self, text="show_msg") dialog.setWindowTitle("clicked") ret = dialog.exec() # Stores the return value for the button pressed MainWindow.connect(self.ui.pushButton, SIGNAL('clicked()'), show_msg) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) 5 系列文章

PyQT6关联信号槽 (六) 百篇文章学PyQT6_pyqt6 connect_双子座断点的博客-CSDN博客

PySide创建界面关联项目(五) 百篇文章学PyQT_双子座断点的博客-CSDN博客

PyCharm运行PyQT6 (四) 百篇文章学PyQT_pycharm pyqt6_双子座断点的博客-CSDN博客

PyQT6 pip install (三) 百篇文章学PyQT_pip install pyqt_双子座断点的博客-CSDN博客

Anaconda3安装部署(二) 百篇文章学PyQT_anaconda3 pyqt_双子座断点的博客-CSDN博客

PyCharm安装部署(一) 百篇文章学PyQT_pycharm 部署_双子座断点的博客-CSDN博客



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3