Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Pyinstaller--不编译我的GUI脚本_Python 3.x_Pyqt5_Pyinstaller - Fatal编程技术网

Python 3.x Pyinstaller--不编译我的GUI脚本

Python 3.x Pyinstaller--不编译我的GUI脚本,python-3.x,pyqt5,pyinstaller,Python 3.x,Pyqt5,Pyinstaller,在过去的几天里,我一直在玩弄pyinstaller和cx_freeze,试图让我的应用程序以.exe格式运行 在python中,一切都按预期工作 我在一个文件夹中准备编译以下项目 rawCodes.py是我的主要代码 py是我的PyQt5 GUI mcnclogo_rc.py包含我在GUI中使用的所有图像 以下是我在我的主目录中的导入列表 import sqlite3 from mcnc import Ui_MainWindow import mcnclogo_rc from PyQt5 im

在过去的几天里,我一直在玩弄pyinstaller和cx_freeze,试图让我的应用程序以.exe格式运行

在python中,一切都按预期工作

我在一个文件夹中准备编译以下项目

rawCodes.py是我的主要代码

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)

是否有人知道如何在我的规范文件中特别列出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
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
然后我仍然得到以下错误

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
这是我的代码和导入

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运行,它就无法再看到数据库中的表

编辑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
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
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
出现直接错误,根本无法连接到数据库。
db文件夹现在指向C:drive。

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

input()

这应该可以解决问题。

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

在进一步检查后,我注意到在编译运行之后,codes.db丢失了,直到我单击.exe运行。然后从exe中弹出一个codes.db,其中包含0kb且没有表

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


我用原始的.db重写空的.db来测试这个问题,突然间我的.exe工作得很好。

我相信答案很简单。让我们分析一下stacktrace

红色部分是错误名称:OperationalError。从:

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

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

没有这样的桌子:不锈钢的

这仅仅意味着该表没有被找到,因为它要么被删除,要么根本没有被创建


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

我不认为您的代码没有正文,但OP认为重现问题所需的信息只是它使用的模块的导入。我在rawCodes.py(我的主py)中有近1000行代码。它在python中运行得非常好。在我的dist文件夹中,我根本看不到对mcnc.py的任何引用……这正常吗?有没有一种方法可以在命令行或规范文件中指定包含其他两个脚本?这并不是我所想的。在modulegraph交叉引用中,我可以看到所有脚本都存在并正确绑定。我想这只是我的Sqlite3数据库现在出现了问题…我在上面发布了一个错误快照。使用.db的完整路径。如果.db与python脚本位于同一目录中,它不需要完整路径,不是吗?如果.exe将安装在不同位置的不同PC上,我如何设置完整路径…当然.db的路径会随位置的不同而变化?首先,按照要求排除问题,然后,我将向您展示如何创建正确的相对路径。感谢您在我下班回家后立即执行。阅读本文,我还鼓励您通过sqlalchemy使用sqlite。我对dbs没有任何问题,也没有修复隐藏导入的问题。这个问题就是一个例子:
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