0

I'm a new programmer, sorry If my question is too basic

I created a mybutton.ui file in Qt Designer, Then I want to run it through my python app. the mybutton.ui file is next to Qt-Designer-GUI-Code.py file. and this is my code:


    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5 import uic
    import sys

    class UI(QWidget):
        def __init__(self):
        super().__init__()
        uic.loadUi("mybutton.ui", self)

    app = QApplication(sys.argv)
    window = UI()
    window.show()
    app.exec__()

when I run the app I see this error:

PC1:Coursera ashkan$ python3 -u "/Users/ashkan/Desktop/Coursera/GUI/tempCodeRunnerFile.py"
Traceback (most recent call last):
  File "/Users/ashkan/Desktop/Coursera/GUI/tempCodeRunnerFile.py", line 11, in <module>
    window = UI()
  File "/Users/ashkan/Desktop/Coursera/GUI/tempCodeRunnerFile.py", line 8, in __init__
    uic.loadUi("mybutton.ui", self)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PyQt5/uic/__init__.py", line 238, in loadUi
    return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PyQt5/uic/Loader/loader.py", line 66, in loadUi
    return self.parse(filename, resource_suffix)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PyQt5/uic/uiparser.py", line 1020, in parse
    document = parse(filename)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/xml/etree/ElementTree.py", line 1229, in parse
    tree.parse(source, parser)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/xml/etree/ElementTree.py", line 569, in parse
    source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'mybutton.ui'

I checked my code and it was completely look like the training I follow. I searched the web and I couldn't solve it.

  • Relative paths are always relative to the working directory. If you are in `/someDir/` and run `python path/to/script.py`, the working dir will still be `/someDir/` and will not find the ui file, because it's in `/someDir/path/to/`. Either go in the directory that contains both files, or use `os.path` or the pathlib functions to create the absolute path from the `__file__`. – musicamante Nov 19 '22 at 19:23
  • Assuming your ui file is in the same direcory as your main script: `import os; uic.loadUi(os.path.join(os.path.dirname(os.path.abspath(__file__)), "mybutton.ui"), self)`. – ekhumoro Nov 19 '22 at 21:59
  • @Alexander no, there's no certainty of that: `uic` functions have absolutely no context of the script they've been called from, if the path is relative they can only reference the working dir, and if the script or the interpreter is run from another directory, the resulting path will be wrong (which is exactly the case of the OP). The only **safe** way to get the path relative to the script is to dynamically provide the absolute path using functions provided by `os.path` or the pathlib, using `__file__` as reference. – musicamante Nov 20 '22 at 00:52

0 Answers0