question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Import behaviour if no Qt bindings are installed

See original GitHub issue

Hi, firstly thanks very much for QtPy - the flexibility it brings has been very useful for a recent Qt gui I’ve been working on. While testing that project, I noticed something which I think is unintended behaviour with qtpy.

This concerns only the case where no Qt bindings are installed. In a clean venv, with only qtpy, pip, wheel and setuptools installed, with the current version of qtpy,

>>> import qtpy
>>> qtpy.API_NAME
'PyQt5'

importing the package succeeds and the API_NAME is set to “PyQt5”, even though there are no Qt bindings installed. Also e.g.:

>>> import qtpy.QtCore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\test_qtpy\qtpy\qtpy\QtCore.py", line 15, in <module>
    from PyQt5.QtCore import *
ModuleNotFoundError: No module named 'PyQt5'

results in a ModuleNotFoundError because the program is looking for PyQt5, rather than raising a QtBindingsNotFoundError.

  • I think that this can be fixed by replacing line 259 in __init__.py with “raise QtBindingsNotFoundError”

  • There also appears to be a bug in the definition of QtBindingsNotFoundError: in line 84 in __init__.py, a “self.” needs to be inserted before _msg.

With these two changes:

>>> import qtpy
Traceback (most recent call last):
  File "E:\test_qtpy\lib\site-packages\qtpy\__init__.py", line 252, in <module>
    from PySide6 import __version__ as PYSIDE_VERSION  # analysis:ignore
ModuleNotFoundError: No module named 'PySide6'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\test_qtpy\lib\site-packages\qtpy\__init__.py", line 259, in <module>
    raise QtBindingsNotFoundError
qtpy.QtBindingsNotFoundError: No Qt bindings could be found
  • As __init__.py is executed before importing any other modules, an attempted import of any other module will result in the QtBindingsNotFoundError
  • I think this means it may be unnecessary for other modules to import the QtBindingsNotFoundError, as this case would be already handled by __init__.py.

Hope this might be useful. Thanks again.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
CAM-Gerlachcommented, Sep 22, 2022

Sounds like a good plan to me 👍

Do you see any advantage on keeping the current behavior?

Nope, not really, other than theoretical backward compatibility with a likely narrow corner case (that is really an unintended bug). I do think at least for 3.0 we should refactor the __init__ along the lines of a simplified #156 to avoid these sorts of issues in the future.

0reactions
dalthvizcommented, Sep 20, 2022

So I think there are already a couple of things for v2.2.1, maybe we should fix the missing self of the _msg call on the QtBindingsNotFoundError definition and release v2.2.1 and then restore raising the exception (although now it will be QtBindingsNotFoundError) for v2.3.0?

What do you think @CAM-Gerlach ? Do you see any advantage on keeping the current behavior?

Read more comments on GitHub >

github_iconTop Results From Across the Web

qtpy.PythonQtError: No Qt bindings could be found - Super User
Keep a native python installed on your machine. Run "pip install pyqt5-tools" from cmd. You should have a message on screen post-installation - ......
Read more >
qtpy.PythonQtError: No Qt bindings could be found · Issue #3545
I am facing the same issue when trying to install spyder in a python2.7 virtualenv: There's nothing we can do about this in...
Read more >
Failed to import any qt binding - Spyder on Windows 10
Show activity on this post. I tried every solution I found (installing PyQt5 and importing it, reinstalling matplotlib, updating pip) and ...
Read more >
PyQt5 - PyPI
PyQt5 is a comprehensive set of Python bindings for Qt v5. It is implemented as more than 35 extension modules and enables Python...
Read more >
Python and PyQt: Building a GUI Desktop Calculator
PyQt is a Python binding for Qt, which is a set of C++ libraries and ... way to install PyQt6 and start using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found