无法在python中创建成功的exe文件[重复]

Posted

技术标签:

【中文标题】无法在python中创建成功的exe文件[重复]【英文标题】:Cannot create a successful exe file in python [duplicate] 【发布时间】:2019-12-07 12:10:55 【问题描述】:

我的 python 项目使用了很多依赖项。我正在使用 firestore、pyqt5、numpy 和其他一些。我尝试使用 Pyinstaller 创建一个可执行文件,但每当我运行该文件时,它都会显示“无法执行脚本。 我也在我的 main.py 文件中使用 images 文件夹和 2 个其他 python 文件作为导入 我尝试使用隐藏的导入,但仍然无法正常工作。有什么简单的方法可以创建一个pyqt5+firestore项目吗?

main.py

from PyQt5 import QtWidgets

from HomePage import Ui_HomePage
from attendance import Ui_Dialog


class Firstwindow(QtWidgets.QMainWindow, Ui_HomePage):
    def __init__(self, parent=None):
        super(Firstwindow, self).__init__(parent)
        self.setupUi(self)
        self.viewAttendance.clicked.connect(self.hide)


class Secondwindow(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super(Secondwindow, self).__init__(parent)
        self.setupUi(self)
        self.backButton.clicked.connect(self.hide)


class Manager:
    def __init__(self):
        self.first = Firstwindow()
        self.second = Secondwindow()

        self.first.viewAttendance.clicked.connect(self.second.show)
        self.second.backButton.clicked.connect(self.first.show)

        self.first.show()


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    manager = Manager()
    sys.exit(app.exec_())

出席.py

from PyQt5 import QtCore, QtGui, QtWidgets
import firebase_admin
import google.cloud
from firebase_admin import credentials, firestore
cred = credentials.Certificate("******")
app1 = firebase_admin.initialize_app(cred)


"""Creating a database and authorization object"""
DB = firestore.client()
ids = []
names = []
age = []
job = []
phone = []
year = []
date = []
shiftStart = []
shiftEnd = []
store = firestore.client()
peopleRef = store.collection(u'People')
salaryRef = store.collection('Salary')
jobRef = store.collection('Jobs')
recordRef = store.collection('Records')
dates  = recordRef.stream()
for dat in dates:
    x = store.collection('Records').document(dat.id).collection('Present')
    idx = x.stream()
    for i in idx:
        print(i.id)
        print(i.to_dict()['Name'])

jo = jobRef.stream()
for doc in jo:
    fo = doc.id
    print(fo)
def getData():
    try:
        docs = peopleRef.stream()
        for doc in docs:
            ids.append(doc.id)
            names.append(doc.to_dict()['Name'])
            job.append(doc.to_dict()['Age'])
            age.append(doc.to_dict()['Job Type'])
            phone.append(doc.to_dict()['Phone'])
    except google.cloud.exceptions.NotFound:
        print(u'Missing data')
    return ids,names,job,age,phone


def clearData():
    ids.clear()
    names.clear()
    age.clear()
    job.clear()
    phone.clear()


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(595, 337)
        Dialog.setWindowFlag(QtCore.Qt.WindowContextHelpButtonHint,False)
        self.gridLayout_2 = QtWidgets.QGridLayout(Dialog)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 0, 5, 1, 1)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem1, 0, 3, 1, 1)
        self.dateEdit = QtWidgets.QDateEdit(Dialog)
        self.dateEdit.setStyleSheet("font: 10pt \"Verdana\";")
        self.dateEdit.setObjectName("dateEdit")
        self.gridLayout.addWidget(self.dateEdit, 0, 6, 1, 1)
        self.refreshButton = QtWidgets.QPushButton(Dialog)
        self.refreshButton.setStyleSheet("qproperty-icon:url(:/images/icons/refresh.png);\n"
"background-color: rgb(255, 255, 255);")
        self.refreshButton.setText("")
        self.refreshButton.setIconSize(QtCore.QSize(20, 20))
        self.refreshButton.setFlat(False)
        self.refreshButton.setObjectName("refreshButton")
        self.gridLayout.addWidget(self.refreshButton, 0, 4, 1, 1)
        self.searchDate = QtWidgets.QPushButton(Dialog)
        self.searchDate.setStyleSheet("QPushButton \n"
"    color: rgb(0,0,0);\n"
"    border: 2px solid #555;\n"
"    border-radius: 20px;\n"
"    border-style: outset;\n"
"    background: rgb(215, 252, 255);\n"
"    padding: 5px;\n"
"    font: 75 8pt \"Georgia\";\n"
"    \n"
"\n"
"QPushButton:hover \n"
"    background: rgb(111, 210, 255)\n"
"    \n"
"\n"
"QPushButton:pressed \n"
"    border-style: inset;\n"
"    background: rgb(111, 210, 255)\n"
"    \n"
"")
        self.searchDate.setObjectName("searchDate")
        self.gridLayout.addWidget(self.searchDate, 0, 7, 1, 1)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.backButton = QtWidgets.QPushButton(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.backButton.sizePolicy().hasHeightForWidth())
        self.backButton.setSizePolicy(sizePolicy)
        self.backButton.setStyleSheet("qproperty-icon:url(:/images/icons/back.jpg);\n"
"background-color: rgb(255, 255, 255);")
        self.backButton.setText("")
        self.backButton.setObjectName("backButton")
        self.horizontalLayout.addWidget(self.backButton)
        self.nameEntry = QtWidgets.QLineEdit(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.nameEntry.sizePolicy().hasHeightForWidth())
        self.nameEntry.setSizePolicy(sizePolicy)
        self.nameEntry.setStyleSheet("font: 10pt \"Verdana\";")
        self.nameEntry.setObjectName("nameEntry")
        self.horizontalLayout.addWidget(self.nameEntry)
        self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
        self.searchName = QtWidgets.QPushButton(Dialog)
        self.searchName.setStyleSheet("QPushButton \n"
"    color: rgb(0,0,0);\n"
"    border: 2px solid #555;\n"
"    border-radius: 20px;\n"
"    border-style: outset;\n"
"    background: rgb(215, 252, 255);\n"
"    padding: 5px;\n"
"    font: 75 8pt \"Georgia\";\n"
"    \n"
"\n"
"QPushButton:hover \n"
"    background: rgb(111, 210, 255)\n"
"    \n"
"\n"
"QPushButton:pressed \n"
"    border-style: inset;\n"
"    background: rgb(111, 210, 255)\n"
"    \n"
"")
        self.searchName.setObjectName("searchName")
        self.gridLayout.addWidget(self.searchName, 0, 1, 1, 1)
        self.profileTree = QtWidgets.QTableWidget(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.profileTree.sizePolicy().hasHeightForWidth())
        self.profileTree.setSizePolicy(sizePolicy)
        self.profileTree.setObjectName("profileTree")
        self.gridLayout.addWidget(self.profileTree, 1, 0, 1, 1)
        self.dataTree = QtWidgets.QTableWidget(Dialog)
        self.dataTree.setObjectName("dataTree")
        self.gridLayout.addWidget(self.dataTree, 1, 1, 1, 7)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.dataTree.setColumnCount(0)
        self.dataTree.setRowCount(0)
        self.profileTree.setRowCount(0)
        self.profileTree.setColumnCount(0)
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        self.profileTree.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers)
        self.refreshButton.clicked.connect(self.refresh)
        self.profileTree.cellDoubleClicked.connect(self.showClicked)


    def showClicked(self,row,col):
        idx = self.profileTree.item(row,0).text()
        print(idx)

    def refresh(self):
        profileData = getData()

        self.profileTree.setRowCount(len(profileData[0]))
        self.profileTree.setColumnCount(2)
        self.profileTree.setHorizontalHeaderLabels(('ID', 'Name'))
        for row in range(len(profileData[0])):
            for col in range(2):
                self.profileTree.setItem(row,col,QtWidgets.QTableWidgetItem(profileData[col][row]))

        clearData()

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.searchDate.setText(_translate("Dialog", "Search"))
        self.searchName.setText(_translate("Dialog", "Search"))
import images


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

【问题讨论】:

您是否尝试在启用控制台窗口的情况下创建 exe?它会向您显示错误,这将有助于解决问题。 是的,我试过了,控制台会立即关闭 好的,我再次运行它,它显示“无法在 PATH 上找到 Qt5Core.dll” 【参考方案1】:

好的,经过数小时的研究,我发现了这篇文章 - 'google-cloud-firestore' distribution doesn't get added to PyInstaller build

问题出在 firestore 而不是 PyQt。

【讨论】:

以上是关于无法在python中创建成功的exe文件[重复]的主要内容,如果未能解决你的问题,请参考以下文章

在 C++、QT 中创建 setup.exe [重复]

如何使用 cx_freeze 在 python 中创建 .EXE 文件

更新到 2020 年 5 月版本后无法在 Visual Studio 2019 中创建 .exe 文件

在python中创建的文件应该打印错误消息[重复]

自动在python中创建多个文件夹[重复]

如何从 Windows 控制台调用 .exe (C++) 以在不同目录(或任何目录)中创建文件夹?