【问题标题】:Pyinstaller --- Not compiling my GUI scriptPyinstaller --- 不编译我的 GUI 脚本
【发布时间】: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


【解决方案1】:

几个小时后,我注意到脚本在尝试访问数据库表时失败,而不是在我的编译中的连接语句处失败。

经过进一步检查,我注意到在编译运行后,codes.db 丢失了,直到我单击 .exe 运行。然后从exe中弹出一个codes.db,大小为0kb,没有表。

我已经得出结论,我需要研究如何在编译中包含原始 .db 文件,并且不允许它在没有它的情况下创建一个新的空 .db。

我已经通过用原始 .db 重写空 .db 来测试这一点,突然我的 .exe 可以完美运行。

【讨论】:

【解决方案2】:

我相信答案很简单。我们来分析stacktrace

红色部分是错误名称:OperationalError。来自docs

与数据库操作相关且不一定在程序员控制之下的错误引发的异常,例如发生意外断开连接、未找到数据源名称、无法处理事务等。它是 DatabaseError 的子类。

但是,绿色部分告诉我们:

没有这样的表:不锈钢

这只是意味着找不到该表,因为它已被删除或根本没有创建。

而且,据我所知,找不到表创建代码。这不是 PyInstaller 错误。

【讨论】:

    【解决方案3】:

    您的exe 关闭然后打开,因为您没有正文代码。在您的主代码正文中添加以下内容:

    input()
    

    这应该可以解决问题。

    【讨论】:

    • 我认为您的代码没有正文,但 OP 认为重现问题所需的信息只是它使用的模块的导入。
    • 我在 rawCodes.py(我的主要 py)中有近 1000 行代码。它在 python 中完美运行。
    • 我在 dist 文件夹中根本看不到任何对 mcnc.py 的引用...这正常吗?有没有办法在命令行或规范文件中指定包含其他 2 个脚本?
    • 这不是我想的那样。在模块图交叉引用中,我可以看到所有脚本都存在并正确捆绑。我认为它只是我的 Sqlite3 数据库现在有问题......我发布了上面的错误截图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2016-07-17
    • 2013-07-27
    • 2013-01-24
    • 2021-06-09
    • 2016-06-07
    相关资源
    最近更新 更多