Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PySide2 Module Not Found



  • I am new to QT and am trying to create the tutorial Hello World application using Python and PySide 2 on Windows. When I check my PySide2 installation from the command prompt I see that I have PySide2 Version 5.14.1 installed. I can successfully import PySide2.QtCore. I can even create a QT application window directly from the command prompt. However, when I try to run the same code from a file in the QT Creator I get an error saying ModuleNotFoundError: No module named 'PySide2'. I'm not sure why QT Creator cannot find PySide2 when I know it is installed.


  • Qt Champions 2019

    @BD4L Do you have more than one Python installation? It could be that QtCreator uses a Python installation which does not have PySide2.



  • @BD4L
    As @jsulm says --- you need to look at how your environment for locating PySide2 is set up.

    But, am I late to the party? I didn't even think you could use Creator for PySide2 development? It doesn't have a Python editor or debugger in the IDE, does it? Whenever I use it it insists on making me pick a C++ kit. I use PyCharm or VS Code for PySide2, even if I use the Designer from Creator for creating the .ui file. Am I missing out?


  • Banned

    Being new to Python-Qt you are going to be unaware that using the Designer to create a UI is perhaps one of the worst things you can do for your program. You should always design your GUI using Qt as it was meant to be used unless you are actually needing to port your GUI to numerous language bases -- which is the real purpose of the Designer UI

    I know some folks prefer the bells and whistles of IDE but I have heard from others that most of these have some kind of minor issue associated with python-qt nothing severe just annoying little quirks that would really bother me -- so I do all my coding in Notepad++ as it handles the only 2 things I am concerned about highlighting reserved words and handling the indentation

    Still good luck with getting your QtCreator up and running -- but yes it works with PySide2 and PyQt5 and Qt5



  • Hi all thanks for the replies. I'll try to answer each persons questions thankss:

    @jsulm - I do have a few different python installations, I assumed QT creator would default to look at the one on my main system path rather than ones that I've setup using virtual environments. How can I check in creator which python installation Creator is set to look at?

    @JonB - When I create a new project in QT creator under the application project type I have two default templates for Python projects both of which try to automatically import PySide2 so I assume it's supported. If it's not I can try creating my project in VS code. I'm not sure what functionality I would lose out on using Pyside2 in VS code vs using QT creator.

    @Denni-0 I'm not sure what you mean by 'design your GUI using Qt as it was meant to be used'. Qt Creator seems to be a standard IDE optimized for Qt applications so I'm not sure why it would be any better or worse than Visual Studio. Right now I"m just trying to learn Qt and Pyside but eventually I'll be using them to create a desktop (windows) application that will produce specific output files which will then be loaded into a mobile (android) app. Qt appears to be one of the languages that will work to accomplish both of those things with python, but I'm open to other suggestions.



  • Just a quick update. I ran the pip install for PySide2 in VS Code in one of my virtual environments and now the PySide2 import works just fine in QT Creator. I would be interested to know how to change what environment QT creator is looking at though. I haven't been able to find anything in the documentation.



  • @BD4L
    The Qt Creator I have is 4.8.1, which is based on the Qt 5.12.2 which was released packaged with the Ubuntu 19.04 I use. It still has nothing about Python/PySide2 in it! So if yours has support for PySide2 projects I haven't seen it! I am "dubious" about how good its Python editing and debugging support will be, but that's me being cynical, I have no evidence for this :) If you are happy with those features then go ahead, a fully integrated, single environment is nice.

    For my part I have used PyCharm with PyQt and VS Code with PySide2. I find PyCharm a much better editing & debugging experience compared to VS Code, which personally I have been disappointed with and don't much like, but your mileage may vary. They all work fine, though, and are much preferable to a plain text editor and no IDE. In both cases if you want to design the layout in Qt Creator/Designer you have to do that outside of PyCharm/VS Code. As I said, if whatever Creator's support for Python is you are happy with it then go for it.

    Your comments about Creator being a standard-ish IDE, for use with Qt, are quite correct. It has its pros & cons like any IDE. Designer does not produce any code, it just generates a .ui file describing exactly the layout you have made. It is the pyuic program supplied with PySide2/PyQt which generates Python code from the .ui. You do not even have to use it or generate any code if you do not want to, you could load the .ui directly at run-time. Though I would recommend you do use it to generate code, it's easier to manipulate with the resulting class/code. Qt was always as "meant to be used" via the Designer as with direct, dynamic widget creation. The code pyuic generates is fine, and perfectly intelligible, and corresponds to the C++ code which would be produced from uic.

    I have done both non-Designer dynamic creation and static creation via uic generated code. Again, pros & cons to each. Certainly as a beginner to Qt the Designer gives you a good start.

    Both PySide2 and PyQt are essentially just Python language bindings to the supplied Qt C++ libraries, offering one-to-one correspondence against the methods exported from Qt. No more and no less.


  • Banned

    Okay here is a very simplistic window that you should be able to create real easily using your Designer. Once you have created the UI and converted to Python-Qt compare the 2 programs and see the major differences between the two. This example is using Python-Qt as it was meant to be used the UI code will look horrendously different but you should be able to figure out what was not needed and keep in mind static placement in a dynamic setting is not good programming practice. Also always try to K.I.S.S. your projects (Keep It Simple and Smart) -- adding levels of complexity beyond what is actually needed is not a good thing to do. Also if you do not know about MVC Methodology learn it as that can play a big roll in your design no matter what you are creating. Lastly only use black-box software when absolutely necessary and the code created by the designer btw is black-box do not touch code so do not use it unless you have no other choice -- and with it you always have a better choice

    from PyQt5.QtCore    import pyqtSlot
    from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton
    
    class MainWindow(QWidget):
        def __init__(self):
            QWidget.__init__(self)
            self.setWindowTitle('Pusher')
            Left = 500; Top = 200; Width = 225; Hight = 100
            self.setGeometry(Left, Top, Width, Hight)
            # -----
            self.btnPush = QPushButton('Push')
            self.btnPush.clicked.connect(self.Pushed)
            # -----
            HBox = QHBoxLayout()
            HBox.addStretch(1)
            HBox.addWidget(self.btnPush)
            HBox.addStretch(1)
            # -----
            VBox = QVBoxLayout()
            VBox.addStretch(1)
            VBox.addLayout(HBox)
            VBox.addStretch(1)
            # -----
            self.setLayout(VBox)
    
        @pyqtSlot()
        def Pushed(self):
            print('I was pushed')
    
    if __name__ == '__main__':
        MainEventThread = QApplication([])
    
        MainApp = MainWindow()
        MainApp.show()
    
        MainEventThread.exec()
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    


  • @JonB My Qt version is 5.14.1 so maybe the Python support in creator is relatively new. So far its worked and doesn't require me to setup different virtual environments for different Visual studio applications which was causing me a headache. I'm actually more worried about the Qt debugging support than python. I have the backend of my code in python mostly done it's getting the code bound to a GUI that I'm trying to figure out, so hopefully the Qt creator can help me with that part. But thanks for all your advice.

    @Denni-0 I was able to run this code in my project by replacing from PyQt5.QtWidgets with from Pyside2.QtWidgets in the import statment. The rest of the code worked as is. I think the confusion might be that I'm not using Qt Developer just Qt creator, so I'm not trying to get it to auto-generate the code, just using it as an IDE to write my own. I'm always open to ideas and suggestions though. Thanks for all the great info so far.


  • Banned

    Oh right my bad forgot you were using PySide2 however as you seemed to have found all you need to change is the following

    from PySide2.QtCore    import Slot
    from PySide2.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton
    

    The PyQt5 to PySide2 you mentioned but wanted to be sure you made the pyqtSlot to Slot change as well


Log in to reply