过去几天一直在玩pyinstaller和cx_freeze试图让我的应用程序以.exe运行。
在python中,一切都按预期工作。
我在准备编译的文件夹中有以下项目...
rawCodes.py是我的主要。
mcnc.py是我的PyQt5 GUI。
mcnclogo_rc.py包含我在GUI中使用的所有图像。
以下是我主要的进口清单......
import sqlite3
from mcnc import Ui_MainWindow
import mcnclogo_rc
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem,
QDialog, QComboBox
import sys
import math
似乎当我运行pyinstaller时它完成了该过程并按预期生成3个文件夹。
exe显然位于dist文件夹中,并且似乎都有效。
然而,我运行exe,它只是打开然后再次关闭,没有任何东西出现。
我确信我的mcnc.py文件没有包含在内,因此我没有看到GUI(pyqt5)。
有谁知道如何在我的SPEC文件中专门列出mcnc.py以确保它包含在内...与mcnclogo_rc相同。
编辑
我从CMD运行.exe,我得到以下追溯...
G:\Yans work in progress\Yans python\Qt\to
compile\dist\rawCodes>rawCodes.exe
Traceback (most recent call last):
File "rawCodes.py", line 3287, in <module>
File "rawCodes.py", line 22, in __init__
File "rawCodes.py", line 3101, in Load_StainlessDb
sqlite3.OperationalError: no such table: stainless
[13020] Failed to execute script rawCodes
可能是因为我的数据库没有正确地打包到dist / build中吗?
编辑2
我已经更改了sqlite3路径,因此它是动态的,但它仍然给出了完全相同的错误...
import sqlite3
import mcnc
from mcnc import Ui_MainWindow
import mcnclogo_rc
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem,
QDialog, QComboBox
import sys
import math
import os
package_dir = os.path.abspath(os.path.dirname(__file__))
db_dir = os.path.join(package_dir, 'codes.db')
print(db_dir)
conn = sqlite3.connect(db_dir)
c = conn.cursor()
c.execute('')
def stainless_list(self):
stainless_getlist = []
content = 'SELECT grade FROM stainless ORDER BY prefix ASC'
res = conn.execute(content)
for row in res:
stainless_getlist.append(row[0])
conn.close
stainless_getlist.insert(0, "Select...")
self.comboBox_2.clear()
self.comboBox_2.addItems(stainless_getlist)
#print(stainless_getlist)
return
然后我仍然得到以下错误......
G:\Yans work in progress\Yans python\Qt\to
compile\dist\rawCodes>rawCodes.exe
Traceback (most recent call last):
File "rawCodes.py", line 3287, in <module>
File "rawCodes.py", line 22, in __init__
File "rawCodes.py", line 3101, in Load_StainlessDb
sqlite3.OperationalError: no such table: stainless
[14664] Failed to execute script rawCodes
编辑3
这是编译后我的目录路径的打印...
G:\Yans work in progress\Yans python\Qt\to
compile\dist\rawCodes>rawCodes.exe
G:\Yans work in progress\Yans python\Qt\to compile\dist\rawCodes
G:\Yans work in progress\Yans python\Qt\to compile\dist\rawCodes\codes.db
这是我的代码和导入......
import sqlite3
import mcnc
from mcnc import Ui_MainWindow
import mcnclogo_rc
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem,
QDialog, QComboBox
import sys
import math
import os
import os.path as op
try:
this_file = __file__
except NameError:
this_file = sys.argv[0]
this_file = op.abspath(this_file)
if getattr(sys, 'frozen', False):
application_path = getattr(sys, '_MEIPASS',
op.dirname(sys.executable))
else:
application_path = op.dirname(this_file)
sqlite_conn = os.path.join(application_path, 'codes.db')
print(application_path)
print(sqlite_conn)
conn = sqlite3.connect(sqlite_conn)
c = conn.cursor()
c.execute('')
但是一旦编译并尝试从dist运行它就不能再看到db中的表了。
编辑4
我将代码更改为以下内容...
import sqlite3
import mcnc
from mcnc import Ui_MainWindow
import mcnclogo_rc
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem,
QDialog, QComboBox
import sys
import math
import os
import os.path as op
try:
this_file = __file__
except NameError:
this_file = sys.argv[0]
this_file = op.abspath(this_file)
if getattr(sys, 'frozen', False):
application_path = getattr(sys, '_MEIPASS', op.dirname(sys.executable))
else:
application_path = op.dirname(this_file)
sqlite_conn = os.path.join(QApplication.applicationDirPath(), 'codes.db')
print(application_path)
print(sqlite_conn)
conn = sqlite3.connect(sqlite_conn)
c = conn.cursor()
c.execute('')
我现在得到以下追溯......
G:\Yans work in progress\Yans python\Qt\to
compile\dist\rawCodes>rawCodes.exe
QCoreApplication::applicationDirPath: Please instantiate the QApplication
object first
G:\Yans work in progress\Yans python\Qt\to compile\dist\rawCodes
codes.db
Traceback (most recent call last):
File "rawCodes.py", line 3303, in <module>
File "rawCodes.py", line 38, in __init__
File "rawCodes.py", line 3117, in Load_StainlessDb
sqlite3.OperationalError: no such table: stainless
[3268] Failed to execute script rawCodes
编辑5
app = QApplication(sys.argv)
sqlite_conn = os.path.join(QApplication.applicationDirPath(), 'codes.db')
G:\Yans work in progress\Yans python\Qt
C:/Program Files (x86)/Python37-32\codes.db
有一个直接的错误,根本无法连接到数据库。 db文件夹现在指向C:驱动器。
然后你的exe
关闭,因为你没有身体代码。在主代码的正文中添加:
input()
那应该解决它。
几个小时后,我注意到脚本在尝试访问db的表而不是在我的编译中的connect语句时失败了。
进一步检查后,我注意到编译运行后,codes.db丢失,直到我点击.exe运行。然后一个codes.db弹出exe的0kb而没有表。
我得出结论,我需要研究如何在编译中包含原始.db文件,并且不允许它在缺少时创建一个新的空.db文件。
我已经通过用原始的.db编写空的.db来测试这个,突然我的.exe工作得很好。