Import behaviour if no Qt bindings are installed
See original GitHub issueHi, 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:
- Created a year ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Sounds like a good plan to me 👍
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.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 theQtBindingsNotFoundError
definition and release v2.2.1 and then restore raising the exception (although now it will beQtBindingsNotFoundError
) for v2.3.0?What do you think @CAM-Gerlach ? Do you see any advantage on keeping the current behavior?