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

PySide2 loadUiTools returns None on Windows 10.



  • I am not sure if this the correct forum to post this but when I try using PySide2's loadUiType on Windows 10 it just returns None. I'm not sure if its in issue with my machine or a bug in PySide2's Windows build. I installed PySide2 using pip. If I build the UI from scratch in Python everything works fine.

    calculator.py

    import sys
    import PySide2
    import PySide2.QtUiTools
    
    print(PySide2)              #<module 'PySide2' from 'C:\\Python\\Python39\\lib\\site-packages\\PySide2\\__init__.py'>
    print(PySide2.QtUiTools)    #<module 'PySide2.QtUiTools' from 'C:\\Python\\Python39\\lib\\site-packages\\PySide2\\QtUiTools.pyd'>
    print (sys.version_info)    #sys.version_info(major=3, minor=9, micro=0, releaselevel='final', serial=0)
    print (PySide2.__version__) #5.15.2
    
    print(PySide2.QtUiTools.loadUiType("C:\calculator.ui"))  #None
    

    calculator.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Form</class>
     <widget class="QWidget" name="Form">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>400</width>
        <height>300</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Form</string>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QPushButton" name="pushButton">
         <property name="text">
          <string>PushButton</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
     <resources/>
     <connections/>
    </ui>
    


  • @Paul-E I added C:\Python\Python39\Scripts (path to pyside2-uic.exe) to my PATH and now it is working. I did not need the Qt libs after all.



  • @Paul-E said in PySide2 loadUiTools returns None on Windows 10.:

    print(PySide2.QtUiTools.loadUiType("C:\calculator.ui")) #None

    Look up how Python handles that single \ in your literal string! You are not passing the path correctly....



  • @JonB that is not the problem. Look up python's escape characters, \c is not an escape character.

    print("C:\calculator.ui")      #C:\calculator.ui
    print(r"C:\calculator.ui")     #C:\calculator.ui
    print(os.path.exists("C:\calculator.ui"))    #True
    print(os.path.exists(r"C:\calculator.ui"))   #True
    


  • @Paul-E
    Sorry, you are right. I did not realise Python treats \ as literal if the next character is not a legitimate escape char. I still think you'd be better with r"C:\calculator.ui" in case you change it to r"C:\nalculator.ui"! :)

    I tried your two files under my Ubuntu 20.04, Qt 5.12, PySide2 5.15. I have never used loadUiType() myself, but I got a pretty strange error message which I cannot explain:

    jon@ubuntu-20:~/QtTests$ python3 calculator.py 
    <module 'PySide2' from '/home/jon/.local/lib/python3.8/site-packages/PySide2/__init__.py'>
    <module 'PySide2.QtUiTools' from '/home/jon/.local/lib/python3.8/site-packages/PySide2/QtUiTools.abi3.so'>
    sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)
    5.15.0
    Error while compiling the generated Python file
      File "<stdin>", line 1
        /********************************************************************************
        ^
    SyntaxError: invalid syntax
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "calculator.py", line 10, in <module>
        print(PySide2.QtUiTools.loadUiType("calculator.ui"))  #None
    SystemError: <built-in function loadUiType> returned a result with an error set
    jon@ubuntu-20:~/QtTests$ 
    

    I don't know where it's finding that C++ comment line from, and I don't know why it's even mentioning stdin.

    I came across the following in https://codereview.qt-project.org/c/pyside/pyside-setup/+/302757

    Use pyside2-uic instead of uic for the loadUiType

    Since we deploy the pyside2-uic wrapper inside the

    bin/ directory of virtual environments, that takes care

    of using the 'uic' binary we ship with the wheels,

    which is located in site-packages/PySide2/.

    Don't know if there is a clue there. So I tried uic -g python calculator.ui. That worked OK, generating a good C++ file for the .ui. But that is also where the /****** ... comes from, as the first line. Looks like something is running that and then trying to interpret that as Python instead of C++??

    I accept that PySide2 brought back loadUiType() as of 5.14. But just maybe there is still some issue? Have a read of https://stackoverflow.com/questions/56658314/pyside2-equivalent-of-pyqt5s-loaduitype-to-dynamically-mix-in-ui-designs.

    In a word: I don't know what's going on with uic -g python calculator.ui. But if you run pyside2-uic calculator.ui it generates Python instead of C++. I can only leave you with that observation...



  • Thank you for taking a second another look.

    Interesting, it seems like on ubuntu you are at least getting some kind of error message. That is way better than getting None(with no errors) on windows.

    I saw that stackoverflow link as well, which seemed to imply the problem would be solved after 5.14.2. Which is why I decided to post here.

    The codereview link is new to me (and appears to be newer than the stackoverflow link). I think I am running into the issue they are describing. I do not have the C++ Qt library installed. I am only using PySide2. Maybe if I install the library it will work (or more likely it will fail exactly where yours is).

    I currently am using the pyside2-uic as a workaround. I was just hoping I could skip that step. (I use to use loadUiType a few years ago just fine so I was hoping to get it working again)

    I do not know if it is worth escalating this as a bug, or I could just ignore it and continue using pyside2-uic as a workaround.



  • I just installed the c++ Qt 5.14.2 libraries and am still just getting None as my return.

    I also made sure the newly installed uic was in my Path variable.
    I also just noticed the codereview link says the patch has been merged. It seems like Qt is under the impression it is working properly.



  • @Paul-E I added C:\Python\Python39\Scripts (path to pyside2-uic.exe) to my PATH and now it is working. I did not need the Qt libs after all.



  • @Paul-E
    Well done, so it was indeed an issue about correctly locating pyside2-uic, I thought it might be. What seems a shame is that you received no kind of error/warning message from loadUiType() that it could not be found, making the task of resolving difficult....


Log in to reply