2

I am using following code to load a ui file, but keep seeing an error message.


# main.py
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic

form_class = uic.loadUiType("main_window.ui")[0]

class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()

Error message:
    FileNotFoundError: [Errno 2] No such file or directory: 'main_window.ui'

main_window.ui is located in the same folder as main.py

2
  • what directory are you executing the file from? Commented Mar 25, 2018 at 16:22
  • I am using desktop folder. That is, both are on(in) a desktop folder. Anyway, when I used "C:\Anaconda3" folder, I had a same issue. Commented Mar 25, 2018 at 17:46

1 Answer 1

2

The name of the file you pass to loadUiType is relative to the working directory, not your python file. You can pass the full path instead. To get the full path, you can find the directory of your current file and then join that with the name of your UI file.

e.g:

...
ui_path = os.path.dirname(os.path.abspath(__file__))
form_class = uic.loadUiType(os.path.join(ui_path, "main_window.ui"))[0]
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I will include the full path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.