I am trying to make an executable (.exe) file of a PyQt5 GUI. The code runs without any problem in PyCharm. The code imports a .ui file and another .py file. It basically has the same format as below.
import os
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QFileDialog, QListWidgetItem
import PyQt5.uic as uic
from PyQt5 import QtCore
import numpy as np
loaded_ui = uic.loadUiType("tester_layout.ui")[0]
class MainGUIobject(QtWidgets.QMainWindow, loaded_ui):
def __init__(self, parent=None): # Initialization of the code
QtWidgets.QMainWindow.__init__(self, parent)
super(MainGUIobject, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.randomFunction)
def randomFunction(self):
self.lineEdit.setText(str("Hello world!"))
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainGUIobject()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedWidth(500)
widget.setFixedHeight(300)
widget.setWindowTitle("pyinstaller_tester")
widget.show()
# sys.exit(app.exec_())
However, when I use Pyinstaller to pack them, the generated .exe file opens up and just closes without opening the GUI. When I tried to run this on the terminal I get an error message saying
Traceback (most recent call last): File "tester.py", line 36, in File "PyQt5\uic_init_.py", line 198, in loadUiType File "PyQt5\uic\Compiler\compiler.py", line 110, in compileUi File "PyQt5\uic\uiparser.py", line 1013, in parse File "xml\etree\ElementTree.py", line 1202, in parse File "xml\etree\ElementTree.py", line 584, in parse FileNotFoundError: [Errno 2] No such file or directory: 'tester_layout.ui' [17800] Failed to execute script 'tester' due to unhandled exception!
I did some research and found this page(PyInstaller + UI Files - FileNotFoundError: [Errno 2] No such file or directory:) which seems like a good solution but I am having a hard time implementing it to my own code. I wasn't sure how to use the resource_path function. So, far this is what I tried, and doesn't work even in PyCharm (The current error that I get is "[Errno 2] No such file or directory: 'C'").
Is there a way to fix this problem
import os
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QFileDialog, QListWidgetItem
import PyQt5.uic as uic
from PyQt5 import QtCore
import numpy as np
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
UI = resource_path("python_image_inspector_layout.ui")[0]
loaded_ui = uic.loadUiType(UI)
class MainGUIobject(QtWidgets.QMainWindow, loaded_ui):
def __init__(self, parent=None): # Initialization of the code
QtWidgets.QMainWindow.__init__(self, parent)
super(MainGUIobject, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.randomFunction)
def randomFunction(self):
self.lineEdit.setText(str("Hello world!"))
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainGUIobject()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedWidth(500)
widget.setFixedHeight(300)
widget.setWindowTitle("pyinstaller_tester")
widget.show()
# sys.exit(app.exec_())
I'm pretty new to PyQt5 and making GUIs. I would love any kind of help.