Search code examples
pythonpyqt5qt-designerpyside2pyside6

NameError: Error evaluating `PySide6.QtWidgets.QWidget.__init__`: name 'PySide6' is not defined


I am trying to run the main.py which has the following code :

import os
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from PySide6 import *




########################################################################
# IMPORT GUI FILE
from ui_Dashboard_Finance import *
########################################################################

########################################################################
# IMPORT Custom widgets
from Custom_Widgets.Widgets import *
########################################################################



class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

       
        # self = QMainWindow class
        # self.ui = Ui_MainWindow / user interface class
        loadJsonStyle(self, self.ui)
       

        self.show()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

When i run the above code in the virtual environment when i have installed all the dependencies PySide2 and PySide6, its gives the following error, i have tried to import every libary which might create the problem, but still nothing improved. The OS i am using is Windows 11. The detailed error is as follows:

File "f:\Development\desktop\OCR_Keyboard\main.py", line 62, in window = MainWindow() File "f:\Development\desktop\OCR_Keyboard\main.py", line 42, in init self.ui.setupUi(self) File "f:\Development\desktop\OCR_Keyboard\ui_Dashboard_Finance.py", line 56, in setupUi self.centralwidget = QWidget(MainWindow) NameError: Error evaluating PySide6.QtWidgets.QWidget.__init__: name 'PySide6' is not defined


Solution

  • I have solved this issue by updating the imports in both main.py and generated file (in my case: ui_Dashboard_Finance) to ensure consistency between the versions of PySide6 or PySide2 being used:

    main.py
    from PySide2 import *
    
    ui_Dashboard_Finance
    from PySide2.QtCore import *
    from PySide2.QtGui import *
    from PySide2.QtWidgets import *
    

    So, the final findings are: if you are using either PySide6 or PySide2 in the generated Python script, you must use the same version of PySide6 or PySide2 in main.py.