0

I am trying to build the standalone PyQt5 application, but wondering how I could properly set the ui file path. I use loadUi from PyQt5.uic, and the pathlib.Path('welcome.ui').resolve() to provide the full path to my Python script. I can open the script, but after building, there's an error saying the welcome.ui file is not found. The command to install:

pyinstaller --onefile --windowed --noconsole main.py

Since I am going to put the building task into the pipeline and Github, the absolute path only solves the problem on my machine. Two files are in the same folder. Is there any way to deal with it? Thanks.

Here is the code:

import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
from pathlib import Path

welcome = Path('welcome.ui')

class WelcomeScreen(QDialog):
  ''' Form a object by inheriting the QDialog'''
  def __init__(self):
    super(WelcomeScreen, self).__init__() # instantiate the object
    loadUi(welcome.resolve(), self)

# main
app = QApplication(sys.argv)
welcome = WelcomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.setFixedHeight(800)
widget.setFixedWidth(1200)
widget.show()

try:
  sys.exit(app.exec_())
except:
  print("Existing")

Here is the XML code created by Qt designer:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1159</width>
    <height>771</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QWidget" name="bgWidget" native="true">
   <property name="geometry">
    <rect>
     <x>-1</x>
     <y>-1</y>
     <width>1200</width>
     <height>800</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">QWidget#bgWidget {
background-color:qlineargradient(spread:pad, x1:0.04, y1:0.142, x2:1, y2:1, stop:0 rgba(129, 255, 192, 255), stop:1 rgba(255, 255, 255, 255)); }</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
Woden
  • 1,054
  • 2
  • 13
  • 26
  • 1
    You can use `--add-data` to add data files into the bundled executable, like `--add-data "welcome.ui;."` . If you use `--onefile`, you can to see [this question](https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile) for details. – gfdsweds Aug 31 '21 at 10:34
  • Thanks for the information. In my real builds, there are more files. – Woden Aug 31 '21 at 10:49
  • 1
    @gfdsweds I carefully read through all the answers. It's helpful. Thank you, again. – Woden Aug 31 '21 at 13:30
  • I'll add an answer if I confirm that the methodology works in the pipeline as well. – Woden Aug 31 '21 at 13:48

0 Answers0