如何编译使用python 3.8的PyQt5程序 [英] How to compile PyQt5 program that uses python 3.8

查看:128
本文介绍了如何编译使用python 3.8的PyQt5程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个简单的PyQt5程序编译为一个EXE文件.我一直在尝试使用 fbs ,但是我没有运气.还有另一种简单的方法可以做我想做的事吗?

如果建议使用pyinstaller,请向我说明具体如何执行.我对诸如仅使用pyinstaller"之类的模糊指示不太满意.并且他们有一个指向Wiki的链接.这对我没有帮助,因为我已经去过那里并且不了解它.

非常感谢您的帮助.

如果有用,这里是我的main.py文件:

 #-*-编码:utf-8-*-#通过读取ui文件'MainMenu.ui'生成的表单实现##创建者:PyQt5 UI代码生成器5.13.2## 警告!在此文件中所做的所有更改都将丢失!从PyQt5导入QtCore,QtGui,QtWidgetsUi_MainWindow类(对象):def setupUi(self,MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(422,350)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.mmlbl = QtWidgets.QLabel(self.centralwidget)self.mmlbl.setGeometry(QtCore.QRect(30,20,131,41))字体= QtGui.QFont()font.setPointSize(24)font.setItalic(False)self.mmlbl.setFont(font)self.mmlbl.setObjectName("mmlbl")self.Updates_button = QtWidgets.QPushButton(self.centralwidget)self.Updates_button.setGeometry(QtCore.QRect(20,70,181,31))self.Updates_button.setAcceptDrops(False)self.Updates_button.setObjectName("Updates_button")self.fwlbutton = QtWidgets.QPushButton(self.centralwidget)self.fwlbutton.setGeometry(QtCore.QRect(20,110,181,31))self.fwlbutton.setObjectName("fwlbutton")self.rmvprosoftbutton = QtWidgets.QPushButton(self.centralwidget)self.rmvprosoftbutton.setGeometry(QtCore.QRect(20,150,181,31))self.rmvprosoftbutton.setObjectName("rmvprosoftbutton")self.auditbutton = QtWidgets.QPushButton(self.centralwidget)self.auditbutton.setGeometry(QtCore.QRect(20,190,181,31))self.auditbutton.setObjectName("auditbutton")self.basicConfbutton = QtWidgets.QPushButton(self.centralwidget)self.basicConfbutton.setGeometry(QtCore.QRect(220,190,181,31))self.basicConfbutton.setObjectName("basicConfbutton")self.UserGroupsButton = QtWidgets.QPushButton(self.centralwidget)self.UserGroupsButton.setGeometry(QtCore.QRect(220,70,181,31))self.UserGroupsButton.setObjectName("UserGroupsButton")self.malrembutton = QtWidgets.QPushButton(self.centralwidget)self.malrembutton.setGeometry(QtCore.QRect(220,150,181,31))self.malrembutton.setObjectName("malrembutton")self.servsetbutton = QtWidgets.QPushButton(self.centralwidget)self.servsetbutton.setGeometry(QtCore.QRect(220,110,181,31))self.servsetbutton.setObjectName("servsetbutton")self.srchmedbutton = QtWidgets.QPushButton(self.centralwidget)self.srchmedbutton.setGeometry(QtCore.QRect(20,230,181,31))self.srchmedbutton.setObjectName("srchmedbutton")self.quit_button = QtWidgets.QPushButton(self.centralwidget)self.quit_button.setGeometry(QtCore.QRect(300,260,81,41))self.quit_button.setObjectName("quit_button")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0,0,422,18))self.menubar.setObjectName("menubar")self.menuAbout = QtWidgets.QMenu(self.menubar)self.menuAbout.setObjectName("menuAbout")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.actionAbout_Creator = QtWidgets.QAction(MainWindow)self.actionAbout_Creator.setObjectName(" actionAbout_Creator")self.actionHow_To_Use = QtWidgets.QAction(MainWindow)self.actionHow_To_Use.setObjectName(" actionHow_To_Use")self.menuAbout.addAction(self.actionAbout_Creator)self.menuAbout.addAction(self.actionHow_To_Use)self.menubar.addAction(self.menuAbout.menuAction())self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self,MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow","Apple CIDR Script Runner")))self.mmlbl.setText(_translate("MainWindow","Main Menu")))self.Updates_button.setText(_translate("MainWindow","Updates"))self.fwlbutton.setText(_translate("MainWindow",防火墙设置")))self.rmvprosoftbutton.setText(_translate("MainWindow",删除禁止的软件")))self.auditbutton.setText(_translate("MainWindow","Audit System"))self.basicConfbutton.setText(_translate("MainWindow",基本配置")))self.UserGroupsButton.setText(_translate("MainWindow","User/Group Settings")))self.malrembutton.setText(_translate("MainWindow",恶意软件删除")))self.servsetbutton.setText(_translate("MainWindow","Services Settings")))self.srchmedbutton.setText(_translate("MainWindow",搜索禁止的媒体")))self.quit_button.setText(_translate("MainWindow","Quit"))self.menuAbout.setTitle(_translate("MainWindow","Help")))self.actionAbout_Creator.setText(_translate("MainWindow","About Creator")))self.actionHow_To_Use.setText(_translate("MainWindow","How to Use")))如果__name__ =="__main__":导入系统应用= QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QMainWindow()ui = Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_()) 

8/19/2020

我已经能够使用名为 auto-py-to-exe

所有您需要做的就是填写您的信息,然后点击将.PY转换为.EXE

此外,我建议您设置已编译.exe的输出位置,以便可以轻松找到它.

还有一件事.确保执行此操作时,正在处理的项目与python安装在同一硬盘上.例如,如果您在Windows的C:驱动器上安装了python,而python项目位于外部硬盘驱动器上,则它将无法正常工作.将项目移至桌面或C:驱动器上的某些位置.

希望如此,祝你好运!

解决方案

除非您绝对需要python 3.8,否则仍可以使用 fbs 来构建您的应用.

fbs 适用于3.6.x以下的python,因此最直接的&实现这一目标的最快方法是使用虚拟环境.

这是我目前要构建的&分发适用于macOS和Win10的fbs/PyQt5/python36应用程序.我还建议将所有依赖项包括在fbs项目的 ./requirements/base.txt 中,以实现更好的/内置的项目可移植性&文档通过. pip install -r ./requirements/base.txt.

步骤

  1. 安装 python 3.6.x
  2. 安装您选择的虚拟环境
    • pip install virtualenv
  3. 创建并创建使用特定的python版本启动虚拟环境
    • virtualenv -p C:/Python36/python.exe venv
    • 开始./venv/Scripts/activate.bat (您可能必须在 cmd.exe 中运行它)
    • 您的cmd提示符现在应该看起来有所不同(即(venv)C:\ Users> )
  4. 再次运行fbs应用程序以确保python 3.6.x没有编译器问题
    • fbs运行
    • 修复所有新的编译器/语法问题
  5. 如果一切顺利,请尝试再次冻结该应用程序
    • fbs冻结

的问题

I am trying to compile a simple PyQt5 program into a single EXE file. I have been trying to use fbs for this but I have had no luck. Is there another easy way to do what I am trying to do?

And if the suggestion is to use pyinstaller please explain to me how exactly to do it. I don't do well with vague instructions like "Just use pyinstaller" and they have a link to the wiki. that is not helpful to me because I have already gone there and did not understand it.

Any help is greatly appreciated.

If it helps here is my main.py file:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainMenu.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(422, 350)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.mmlbl = QtWidgets.QLabel(self.centralwidget)
        self.mmlbl.setGeometry(QtCore.QRect(30, 20, 131, 41))
        font = QtGui.QFont()
        font.setPointSize(24)
        font.setItalic(False)
        self.mmlbl.setFont(font)
        self.mmlbl.setObjectName("mmlbl")
        self.Updates_button = QtWidgets.QPushButton(self.centralwidget)
        self.Updates_button.setGeometry(QtCore.QRect(20, 70, 181, 31))
        self.Updates_button.setAcceptDrops(False)
        self.Updates_button.setObjectName("Updates_button")
        self.fwlbutton = QtWidgets.QPushButton(self.centralwidget)
        self.fwlbutton.setGeometry(QtCore.QRect(20, 110, 181, 31))
        self.fwlbutton.setObjectName("fwlbutton")
        self.rmvprosoftbutton = QtWidgets.QPushButton(self.centralwidget)
        self.rmvprosoftbutton.setGeometry(QtCore.QRect(20, 150, 181, 31))
        self.rmvprosoftbutton.setObjectName("rmvprosoftbutton")
        self.auditbutton = QtWidgets.QPushButton(self.centralwidget)
        self.auditbutton.setGeometry(QtCore.QRect(20, 190, 181, 31))
        self.auditbutton.setObjectName("auditbutton")
        self.basicConfbutton = QtWidgets.QPushButton(self.centralwidget)
        self.basicConfbutton.setGeometry(QtCore.QRect(220, 190, 181, 31))
        self.basicConfbutton.setObjectName("basicConfbutton")
        self.UserGroupsButton = QtWidgets.QPushButton(self.centralwidget)
        self.UserGroupsButton.setGeometry(QtCore.QRect(220, 70, 181, 31))
        self.UserGroupsButton.setObjectName("UserGroupsButton")
        self.malrembutton = QtWidgets.QPushButton(self.centralwidget)
        self.malrembutton.setGeometry(QtCore.QRect(220, 150, 181, 31))
        self.malrembutton.setObjectName("malrembutton")
        self.servsetbutton = QtWidgets.QPushButton(self.centralwidget)
        self.servsetbutton.setGeometry(QtCore.QRect(220, 110, 181, 31))
        self.servsetbutton.setObjectName("servsetbutton")
        self.srchmedbutton = QtWidgets.QPushButton(self.centralwidget)
        self.srchmedbutton.setGeometry(QtCore.QRect(20, 230, 181, 31))
        self.srchmedbutton.setObjectName("srchmedbutton")
        self.quit_button = QtWidgets.QPushButton(self.centralwidget)
        self.quit_button.setGeometry(QtCore.QRect(300, 260, 81, 41))
        self.quit_button.setObjectName("quit_button")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 422, 18))
        self.menubar.setObjectName("menubar")
        self.menuAbout = QtWidgets.QMenu(self.menubar)
        self.menuAbout.setObjectName("menuAbout")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionAbout_Creator = QtWidgets.QAction(MainWindow)
        self.actionAbout_Creator.setObjectName("actionAbout_Creator")
        self.actionHow_To_Use = QtWidgets.QAction(MainWindow)
        self.actionHow_To_Use.setObjectName("actionHow_To_Use")
        self.menuAbout.addAction(self.actionAbout_Creator)
        self.menuAbout.addAction(self.actionHow_To_Use)
        self.menubar.addAction(self.menuAbout.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Apple CIDR Script Runner"))
        self.mmlbl.setText(_translate("MainWindow", "Main Menu"))
        self.Updates_button.setText(_translate("MainWindow", "Updates"))
        self.fwlbutton.setText(_translate("MainWindow", "Firewall Settings"))
        self.rmvprosoftbutton.setText(_translate("MainWindow", "Remove Prohibited Software"))
        self.auditbutton.setText(_translate("MainWindow", "Audit System"))
        self.basicConfbutton.setText(_translate("MainWindow", "Basic Configurations"))
        self.UserGroupsButton.setText(_translate("MainWindow", "User / Group Settings"))
        self.malrembutton.setText(_translate("MainWindow", "Malware Removal"))
        self.servsetbutton.setText(_translate("MainWindow", "Services Settings"))
        self.srchmedbutton.setText(_translate("MainWindow", "Search For Prohibited Media"))
        self.quit_button.setText(_translate("MainWindow", "Quit"))
        self.menuAbout.setTitle(_translate("MainWindow", "Help"))
        self.actionAbout_Creator.setText(_translate("MainWindow", "About Creator"))
        self.actionHow_To_Use.setText(_translate("MainWindow", "How To Use"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Edit: 8/19/2020

I have been able to successfully compile my python program into an EXE file using a program called auto-py-to-exe https://pypi.org/project/auto-py-to-exe/. This program uses pyinstaller and creates a GUI pyinstaller configuration page in your browser. (This makes setting up your pyinstaller command much easier)

Here is what it looks like when running:

All you will have to do is fill in your information and hit Convert .PY to .EXE

Also, I recommend that you set the output location of the compiled .exe so you can find it easily.

One more thing. Make sure that when you are doing this, the project that you are working on is on the same hard drive that python is installed on. For example, if you have python installed on your C: drive on windows and your python project is on an external hard drive, it will not work. Move your project over to the Desktop or some where on the C: drive.

Hope that made sense, good luck!

解决方案

Unless you absolutely require python 3.8, you can use still use fbs to build your app.

fbs works with python up to 3.6.x, so the most straight forward & quickest way to get it going is to use a virtual environment.

This is currently my workflow to build & distribute fbs/PyQt5/python36 apps for macOS and win10. I also recommend including all dependencies in your fbs project's ./requirements/base.txt for better/built-in project portability & documentation via. pip install -r ./requirements/base.txt.

Steps

  1. Install python 3.6.x
  2. Install virtual environment of your choice
    • pip install virtualenv
  3. Create & start virtual environment with specific python version
    • virtualenv -p C:/Python36/python.exe venv
    • start ./venv/Scripts/activate.bat (you may have to run this in cmd.exe)
    • Your cmd prompt should now look different (ie. (venv) C:\Users>)
  4. Run fbs app again to make sure no compiler issues with python 3.6.x
    • fbs run
    • Fix any new compiler/syntax issues
  5. If all goes well, attempt to freeze the app again
    • fbs freeze

这篇关于如何编译使用python 3.8的PyQt5程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆