'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 1:[1]

I have solved this issue by changing the imports in the generated file of UI i.e. ui_Dashboard_Finance I have changed the following imports in both main.py and ui_Dashboard_Finance

main.py

from PySide2 import *

ui_Dashboard_Finance

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

so final findings are if you are using PySide6 or PySide2 in generated python script and you must to use same PySide6 or PySide2 in main.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ehsan125