• PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件


    PyQt5快速开发与实战

    9. 第9章 PyQt5 扩展应用

    9.1 使用PyInstaller打包项目生成exe文件

    PyInstaller是一个很好用而且免费的打包工具,支持 Windows、Linux、Mac OS,并且支持32位和64位系统。

    官方网站地址

    在这里插入图片描述

    1. 安装环境

      在这里插入图片描述

    2. 安装Pyinstaller

      pip install Pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
      
      • 1

      在这里插入图片描述

    3. Pyinstaller的使用

    pyinstaller [opts] xxx.py
    
    • 1

    可选的参数有:

    • -F,-onefile,打包成一个 EXE文件。
    • -D,-onedir,创建一个目录,包含EXE文件,但会依赖很多文件(默认选项)。
    • -c,-console,-nowindowed,使用控制台,无窗口(默认).
    • -w, -windowed, -noconsole,使用窗口,无控制台。
    1. 打包测试

      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_())
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34

      执行命令

      pyinstaller -F -w colorDialog.py
      
      • 1

      在这里插入图片描述

      在这里插入图片描述

    2. 运行测试

      在这里插入图片描述

      运行时出现了这个错误。

      像是说什么东西没有找到,开始谷歌…

      from PyInstaller.utils.hooks import copy_metadata
      datas = copy_metadata('pyqt5_plugins') + copy_metadata('qt5_tools') + copy_metadata('qt5_applications')
      
      • 1
      • 2

      在路径中加上了这个文件,打包命名改成

      pyinstaller -F -w colorDialog.py --additional-hooks-dir=.
      
      • 1

      中途还报了很多类似于没有模块的错误,我都一个一个的在主文件中import了

      在这里插入图片描述

      最后

      在这里插入图片描述

      两种方式运行对比。一样的

    3. Pyinstaller原理简介

      PyInstaller其实就是把 Python解释器和脚本打包成一个可执行文件,和编译成真正的机器码完全是两回事。所以千万不要指望打包成可执行文件会提高运行效率,相反,可能会降低运行效率,但是这样做的好处是在运行者的机器上不用安装 Python和脚本所依赖的库。在 Linux操作系统下,它主要使用的是 binutil工具包中的ldd和objdump命令。

      输入指定的脚本后,首先 PyInstaller会分析该脚本所依赖的其他依赖,然后进行查找,并复制,把所有相关的依赖都收集起来并进行加密处理,包括Python 解释器,最后把这些文件放在一个目录下,或者打包到一个可执行文件中。然后就可以直接运行所生成的可执行文件,不需要安装其他包或某个版本的Python。需要注意的是,使用 PyInstaller打包生成的可执行文件,只能在和打包器系统相同的环境下运行。也就是说,这个可执行文件不具备可移植性,若需要不同的操作系统上运行,就必须在该系统环境上重新进行打包。

  • 相关阅读:
    从零开始搭建SpringBoot项目,并且用Mybatis进行数据查询(包含所用数据库)
    【数通 | BGP】BGP的基本概念和工作原理
    EFCore分表实现
    Android Studio 直接获取Spinner的值
    C#不安全代码
    husky+lint-staged+eslint+prettier+stylelint+commitlint
    使用Python获取文件中的字符数、单词数、空格数和行数
    [LeetCode 1774]最接近目标价格的甜点成本
    人血白蛋白修饰银纳米簇(HSA-AgNCs),间苯二酚/丹酚酸A偶联人血清白蛋白
    【ffmpeg】YUV实践
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126071019