Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. libQt5Core5 missmatch (beginner)
Forum Updated to NodeBB v4.3 + New Features

libQt5Core5 missmatch (beginner)

Scheduled Pinned Locked Moved Solved Qt for Python
pyside2
16 Posts 6 Posters 3.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Oak77O Oak77

    I also tried to switch to PyQt5 bindings this way:

    import sys
    from PyQt5.QtWidgets import QApplication, uic
    
    if __name__ == "__main__":
        app = QApplication([])
    
        uic.loadUi('mainw.ui')
    
        sys.exit(app.exec_())
    

    ...but I get exactly the same error:

    Traceback (most recent call last):
      File "/home/<user>/Coding/Projects/TestIS/main.py", line 3, in <module>
        from PyQt5.QtWidgets import QApplication, uic
    ImportError: cannot import name 'uic'
    11:04:59: /usr/bin/python3 exited with code 1
    

    I feel like I'm doing something really basic wrong. But I can find a lot of references to this such syntax with importing 'uic' and really can't find what's wrong.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #7

    @Oak77

    from PyQt5.QtWidgets import QApplication, uic

    Where did you get idea of from PyQt5/PySide2.QtWidgets import uic from?

    For PySide2:

    Do you want to read and follow approach from https://doc.qt.io/qtforpython/tutorials/basictutorial/uifiles.html#loading-it-directly ? Essentially

    from PySide2.QtUiTools import QUiLoader
    
    ui_file = QFile("mainwindow.ui")
    ui_file.open(QFile.ReadOnly)
    
    loader = QUiLoader()
    window = loader.load(ui_file)
    window.show()
    

    For PyQt5:

    It's from PyQt5 import uic, not QtWidgets. See https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html & https://www.riverbankcomputing.com/static/Docs/PyQt5/api/uic/uic-module.html

    That's to let you load the .ui file dynamically at run-time. If you want to generate a Python class at design-time, there is (command-line tools) pyside2-uic or pyuic5 for PyQt. Or similar, search for these.

    If you want advice on which approach to take, I would say the design-time generation of classes probably gives you easier access to the UI objects created. You have to get used to running the pyuic bit whenever you change the UI in the designer.

    You probably want to make your decision about whether you want to go PySide2 or PyQt5, and stick with it. I don't know what your criteria are.

    1 Reply Last reply
    3
    • Oak77O Oak77

      I also tried to switch to PyQt5 bindings this way:

      import sys
      from PyQt5.QtWidgets import QApplication, uic
      
      if __name__ == "__main__":
          app = QApplication([])
      
          uic.loadUi('mainw.ui')
      
          sys.exit(app.exec_())
      

      ...but I get exactly the same error:

      Traceback (most recent call last):
        File "/home/<user>/Coding/Projects/TestIS/main.py", line 3, in <module>
          from PyQt5.QtWidgets import QApplication, uic
      ImportError: cannot import name 'uic'
      11:04:59: /usr/bin/python3 exited with code 1
      

      I feel like I'm doing something really basic wrong. But I can find a lot of references to this such syntax with importing 'uic' and really can't find what's wrong.

      jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #8

      @Oak77

      With PyQt5, first you use the pyuic5 tool that will have been installed at the same time as PyQt5 (assuming you used pip) to convert your .ui file into a python module:

      $ pyuic5 -x mainw.ui -o mainw.py

      You then import this module into your main application code and use it thus:

      from PyQt5.QtWidgets import QMainWindow
      from mainw import Ui_MainWindow
      
      class MainWindow(QMainWindow):
          def __init__(self, parent=None, **kwargs):
              super().__init__(parent, **kwargs)
      
              self.ui=UI_MainWindow()
              self.ui.setupUI(self)
      
      if __name__=='__main__':
          from sys import argv, exit
          from PyQt5.QtWidgets import QApplication
      
          a=QApplication(argv)
          m=MainWindow()
          m.show()
          exit(a.exec_())
      

      Hope this helps :o)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      Oak77O 1 Reply Last reply
      2
      • jazzycamelJ jazzycamel

        @Oak77

        With PyQt5, first you use the pyuic5 tool that will have been installed at the same time as PyQt5 (assuming you used pip) to convert your .ui file into a python module:

        $ pyuic5 -x mainw.ui -o mainw.py

        You then import this module into your main application code and use it thus:

        from PyQt5.QtWidgets import QMainWindow
        from mainw import Ui_MainWindow
        
        class MainWindow(QMainWindow):
            def __init__(self, parent=None, **kwargs):
                super().__init__(parent, **kwargs)
        
                self.ui=UI_MainWindow()
                self.ui.setupUI(self)
        
        if __name__=='__main__':
            from sys import argv, exit
            from PyQt5.QtWidgets import QApplication
        
            a=QApplication(argv)
            m=MainWindow()
            m.show()
            exit(a.exec_())
        

        Hope this helps :o)

        Oak77O Offline
        Oak77O Offline
        Oak77
        wrote on last edited by Oak77
        #9

        "With PyQt5, first you use the pyuic5 tool that will have been installed at the same time as PyQt5...."

        @jazzycamel I believe that is one of possible methods (?). I read through tutorials, posts and books and I'd swear I read explicitely, that those are two different ways to use UI file. Also I'm following tutorials for this method.

        I know I can convert, but I deliberately want to use UI files at this stage, because I'd prefer to use them at design stage. When the particular forms are stable, I might get into habit to export them, if it will help performance noticeably.
        I'm not saying I won't end up using this method as standard later, I'm only learning at this stage.

        I'll try the conversion as per your code and instructionss as another training, it won't hurt me :-).

        EDIT:
        I get command-not-found when trying to run:

        pyuic5 -x mainw.ui -o mainw.py
        

        So I'm starting to think those two issues are connected.

        Again, I do have PyQt5 installed:

        > pip install --upgrade PyQt5
        Defaulting to user installation because normal site-packages is not writeable
        Requirement already up-to-date: PyQt5 in /home/<user>/.local/lib/python3.6/site-packages (5.14.1)
        Requirement already satisfied, skipping upgrade: PyQt5-sip<13,>=12.7 in /home/<user>/.local/lib/python3.6/site-packages (from PyQt5) (12.7.1)
        

        ...but the strange "Defaulting to user installation" message might be a clue. My python is in /usr/bin/Python3, my libraries in /usr/lib64/python/ (symlink), but here it looks into user directory /home/<user>/.local/lib/python3.6/site-packages. When I run following command:

        > python -m site
        sys.path = [
            '/home/libor/Coding/Projects/Test2',
            '/usr/lib/python27.zip',
            '/usr/lib64/python2.7',
            '/usr/lib64/python2.7/plat-linux2',
            '/usr/lib64/python2.7/lib-tk',
            '/usr/lib64/python2.7/lib-old',
            '/usr/lib64/python2.7/lib-dynload',
            '/usr/lib64/python2.7/site-packages',
            '/usr/lib64/python2.7/site-packages/PIL',
            '/usr/local/lib64/python2.7/site-packages',
            '/usr/local/lib/python2.7/site-packages',
            '/usr/lib64/python2.7/site-packages/gtk-2.0',
            '/usr/lib/python2.7/site-packages',
            '/usr/local/lib64/python2.7/site-packages',
            '/usr/local/lib/python2.7/site-packages',
        ]
        USER_BASE: '/home/<user>/.local' (exists)
        USER_SITE: '/home/<user>/.local/lib/python2.7/site-packages' (doesn't exist)
        ENABLE_USER_SITE: True
        

        ...I think I see the evil: USER_SITE points to different site-packages, from Python 2.7.

        So can I simply do following??

        export USER_SITE=/home/<user>/.local/lib/python3.6/site-packages 
        

        Does it look like a solution?

        Pablo J. RoginaP JonBJ 2 Replies Last reply
        0
        • Oak77O Oak77

          "With PyQt5, first you use the pyuic5 tool that will have been installed at the same time as PyQt5...."

          @jazzycamel I believe that is one of possible methods (?). I read through tutorials, posts and books and I'd swear I read explicitely, that those are two different ways to use UI file. Also I'm following tutorials for this method.

          I know I can convert, but I deliberately want to use UI files at this stage, because I'd prefer to use them at design stage. When the particular forms are stable, I might get into habit to export them, if it will help performance noticeably.
          I'm not saying I won't end up using this method as standard later, I'm only learning at this stage.

          I'll try the conversion as per your code and instructionss as another training, it won't hurt me :-).

          EDIT:
          I get command-not-found when trying to run:

          pyuic5 -x mainw.ui -o mainw.py
          

          So I'm starting to think those two issues are connected.

          Again, I do have PyQt5 installed:

          > pip install --upgrade PyQt5
          Defaulting to user installation because normal site-packages is not writeable
          Requirement already up-to-date: PyQt5 in /home/<user>/.local/lib/python3.6/site-packages (5.14.1)
          Requirement already satisfied, skipping upgrade: PyQt5-sip<13,>=12.7 in /home/<user>/.local/lib/python3.6/site-packages (from PyQt5) (12.7.1)
          

          ...but the strange "Defaulting to user installation" message might be a clue. My python is in /usr/bin/Python3, my libraries in /usr/lib64/python/ (symlink), but here it looks into user directory /home/<user>/.local/lib/python3.6/site-packages. When I run following command:

          > python -m site
          sys.path = [
              '/home/libor/Coding/Projects/Test2',
              '/usr/lib/python27.zip',
              '/usr/lib64/python2.7',
              '/usr/lib64/python2.7/plat-linux2',
              '/usr/lib64/python2.7/lib-tk',
              '/usr/lib64/python2.7/lib-old',
              '/usr/lib64/python2.7/lib-dynload',
              '/usr/lib64/python2.7/site-packages',
              '/usr/lib64/python2.7/site-packages/PIL',
              '/usr/local/lib64/python2.7/site-packages',
              '/usr/local/lib/python2.7/site-packages',
              '/usr/lib64/python2.7/site-packages/gtk-2.0',
              '/usr/lib/python2.7/site-packages',
              '/usr/local/lib64/python2.7/site-packages',
              '/usr/local/lib/python2.7/site-packages',
          ]
          USER_BASE: '/home/<user>/.local' (exists)
          USER_SITE: '/home/<user>/.local/lib/python2.7/site-packages' (doesn't exist)
          ENABLE_USER_SITE: True
          

          ...I think I see the evil: USER_SITE points to different site-packages, from Python 2.7.

          So can I simply do following??

          export USER_SITE=/home/<user>/.local/lib/python3.6/site-packages 
          

          Does it look like a solution?

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #10

          @Oak77 as a brief summary, there are 3 methods to create your GUIs:

          1. Hand-coded GUI
          2. Tool-coded GUI from .ui file (using pyside2-uic or pyqt-uic)
          3. Loading of .ui file at runtime

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          3
          • Oak77O Oak77

            "With PyQt5, first you use the pyuic5 tool that will have been installed at the same time as PyQt5...."

            @jazzycamel I believe that is one of possible methods (?). I read through tutorials, posts and books and I'd swear I read explicitely, that those are two different ways to use UI file. Also I'm following tutorials for this method.

            I know I can convert, but I deliberately want to use UI files at this stage, because I'd prefer to use them at design stage. When the particular forms are stable, I might get into habit to export them, if it will help performance noticeably.
            I'm not saying I won't end up using this method as standard later, I'm only learning at this stage.

            I'll try the conversion as per your code and instructionss as another training, it won't hurt me :-).

            EDIT:
            I get command-not-found when trying to run:

            pyuic5 -x mainw.ui -o mainw.py
            

            So I'm starting to think those two issues are connected.

            Again, I do have PyQt5 installed:

            > pip install --upgrade PyQt5
            Defaulting to user installation because normal site-packages is not writeable
            Requirement already up-to-date: PyQt5 in /home/<user>/.local/lib/python3.6/site-packages (5.14.1)
            Requirement already satisfied, skipping upgrade: PyQt5-sip<13,>=12.7 in /home/<user>/.local/lib/python3.6/site-packages (from PyQt5) (12.7.1)
            

            ...but the strange "Defaulting to user installation" message might be a clue. My python is in /usr/bin/Python3, my libraries in /usr/lib64/python/ (symlink), but here it looks into user directory /home/<user>/.local/lib/python3.6/site-packages. When I run following command:

            > python -m site
            sys.path = [
                '/home/libor/Coding/Projects/Test2',
                '/usr/lib/python27.zip',
                '/usr/lib64/python2.7',
                '/usr/lib64/python2.7/plat-linux2',
                '/usr/lib64/python2.7/lib-tk',
                '/usr/lib64/python2.7/lib-old',
                '/usr/lib64/python2.7/lib-dynload',
                '/usr/lib64/python2.7/site-packages',
                '/usr/lib64/python2.7/site-packages/PIL',
                '/usr/local/lib64/python2.7/site-packages',
                '/usr/local/lib/python2.7/site-packages',
                '/usr/lib64/python2.7/site-packages/gtk-2.0',
                '/usr/lib/python2.7/site-packages',
                '/usr/local/lib64/python2.7/site-packages',
                '/usr/local/lib/python2.7/site-packages',
            ]
            USER_BASE: '/home/<user>/.local' (exists)
            USER_SITE: '/home/<user>/.local/lib/python2.7/site-packages' (doesn't exist)
            ENABLE_USER_SITE: True
            

            ...I think I see the evil: USER_SITE points to different site-packages, from Python 2.7.

            So can I simply do following??

            export USER_SITE=/home/<user>/.local/lib/python3.6/site-packages 
            

            Does it look like a solution?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #11

            @Oak77 said in libQt5Core5 missmatch (beginner):

            python -m site

            Before you go any further! Under Linux python executes Python 2. That's why you're seeing all those python2.7 directories. You do not want this at all! Everything you want to do (for Qt) you need to use python3 as the command interpreter, for Python 3!!

            Oak77O 1 Reply Last reply
            1
            • JonBJ JonB

              @Oak77 said in libQt5Core5 missmatch (beginner):

              python -m site

              Before you go any further! Under Linux python executes Python 2. That's why you're seeing all those python2.7 directories. You do not want this at all! Everything you want to do (for Qt) you need to use python3 as the command interpreter, for Python 3!!

              Oak77O Offline
              Oak77O Offline
              Oak77
              wrote on last edited by
              #12

              @JonB You're spot on, but I didn't think it was a problem. Running

              > python
              Python 2.7.17 (default, Jan 24 2020, 15:33:58) [GCC] on linux2
              Type "help", "copyright", "credits" or "license" for more information.
              >>> 
              

              ...as oposed to:

              > python3
              Python 3.6.10 (default, Jan 16 2020, 09:12:04) [GCC] on linux
              Type "help", "copyright", "credits" or "license" for more information.
              >>> 
              

              So in terminal, it seems to really run pyuic from 2.7, not 3.6.

              By this time I am totally confused how to make Pythons consistent while maintaining existing dependencies in the OS.

              QtCreator is set to 3.6.10 (Tools --> Options: Python: Interpreters: Make default 3.6.10) as mentioned at the beginnig and the project to run in the same environment (Interpreter = Python 3.6.10).

              What else I can do to get things working?

              I asume that Python 2.7.17 is sitting there due to dependencies of intalled applications.

              jazzycamelJ 1 Reply Last reply
              0
              • Oak77O Oak77

                @JonB You're spot on, but I didn't think it was a problem. Running

                > python
                Python 2.7.17 (default, Jan 24 2020, 15:33:58) [GCC] on linux2
                Type "help", "copyright", "credits" or "license" for more information.
                >>> 
                

                ...as oposed to:

                > python3
                Python 3.6.10 (default, Jan 16 2020, 09:12:04) [GCC] on linux
                Type "help", "copyright", "credits" or "license" for more information.
                >>> 
                

                So in terminal, it seems to really run pyuic from 2.7, not 3.6.

                By this time I am totally confused how to make Pythons consistent while maintaining existing dependencies in the OS.

                QtCreator is set to 3.6.10 (Tools --> Options: Python: Interpreters: Make default 3.6.10) as mentioned at the beginnig and the project to run in the same environment (Interpreter = Python 3.6.10).

                What else I can do to get things working?

                I asume that Python 2.7.17 is sitting there due to dependencies of intalled applications.

                jazzycamelJ Offline
                jazzycamelJ Offline
                jazzycamel
                wrote on last edited by
                #13

                @Oak77
                If you can't find the pyuic5 command itself, but have PyQt5 installed and importable, then run pyuic as a module instead:

                $ python3 -m PyQt5.uic.pyuic -x mainw.ui -o mainw.py
                

                For future reference, I always recomend using a seperate virtualenv for each python project you develop. That way you always know where the libraries/tools are and you don't get cross-contamination or version issues.

                Hope this helps :o)

                For the avoidance of doubt:

                1. All my code samples (C++ or Python) are tested before posting
                2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                Oak77O 1 Reply Last reply
                1
                • jazzycamelJ jazzycamel

                  @Oak77
                  If you can't find the pyuic5 command itself, but have PyQt5 installed and importable, then run pyuic as a module instead:

                  $ python3 -m PyQt5.uic.pyuic -x mainw.ui -o mainw.py
                  

                  For future reference, I always recomend using a seperate virtualenv for each python project you develop. That way you always know where the libraries/tools are and you don't get cross-contamination or version issues.

                  Hope this helps :o)

                  Oak77O Offline
                  Oak77O Offline
                  Oak77
                  wrote on last edited by Oak77
                  #14

                  "python3 -m PyQt5.uic.pyuic -x mainw.ui -o mainw.py"

                  @jazzycamel - That worked and once I see it, I understand why. Thank you very much.

                  I tried one project and set it with virtual environment, but it downloaded some 320 GB of data (for only some dozens of bytes long tutorial). So it can spiral out into significant gigabytes for only few kilobytes I program at the moment, while learning. It seems to me a bit unreasonagle, unless for projects with specific needs. Unless I misunderstood something, which I easily could, at this stage.
                  And I also have to get to undertand how Python is set up anyway, if I want to use it, so I'll am testing both ways.

                  jazzycamelJ 1 Reply Last reply
                  0
                  • Oak77O Oak77

                    "python3 -m PyQt5.uic.pyuic -x mainw.ui -o mainw.py"

                    @jazzycamel - That worked and once I see it, I understand why. Thank you very much.

                    I tried one project and set it with virtual environment, but it downloaded some 320 GB of data (for only some dozens of bytes long tutorial). So it can spiral out into significant gigabytes for only few kilobytes I program at the moment, while learning. It seems to me a bit unreasonagle, unless for projects with specific needs. Unless I misunderstood something, which I easily could, at this stage.
                    And I also have to get to undertand how Python is set up anyway, if I want to use it, so I'll am testing both ways.

                    jazzycamelJ Offline
                    jazzycamelJ Offline
                    jazzycamel
                    wrote on last edited by
                    #15

                    @Oak77
                    I'm not sure why it would download that much data for a virtualenv, I must have 30 or 40+ on my machine, they are basically throw away tiny things.

                    Anyways, I'm glad it worked for you :o)

                    For the avoidance of doubt:

                    1. All my code samples (C++ or Python) are tested before posting
                    2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                    1 Reply Last reply
                    2
                    • Oak77O Offline
                      Oak77O Offline
                      Oak77
                      wrote on last edited by
                      #16

                      To close this thread, this is final working code:

                      import sys
                      from PySide2 import QtWidgets
                      from PySide2.QtUiTools import QUiLoader
                      from PySide2.QtWidgets import QApplication
                      
                      
                      class Form(QObject):
                      
                          def __init__(self, ui_file, parent=None):
                              super(Form, self).__init__(parent)
                              ui_file = QFile(ui_file)
                              ui_file.open(QFile.ReadOnly)
                              
                              self.window.show()
                      
                      
                      if __name__ == '__main__':
                          app = QApplication(sys.argv)
                          form = Form('mainwindow.ui')
                          sys.exit(app.exec_())
                              
                      
                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved