【发布时间】:2019-09-08 06:02:57
【问题描述】:
过去几天一直在玩 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: 驱动器。
【问题讨论】:
-
使用.db的完整路径
-
如果.db与python脚本位于同一目录中,它不需要完整路径吗?如果 .exe 将安装在不同位置的不同 PC 上,我如何设置完整路径...肯定 .db 的路径会根据情况而变化?
-
先做你被告知的排除如果这是问题,然后我会告诉你如何创建正确的相对路径
-
我下班回家后会尽快感谢您。
标签: python-3.x pyqt5 pyinstaller