pytest-dev / pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
https://pytest-qt.readthedocs.io
MIT License
409 stars 70 forks source link

ImportError after uninstalling bindings #473

Closed oscargus closed 1 year ago

oscargus commented 1 year ago

It seems like it is possible that either PyQt6 or PySide6 (not clear which) do not fully uninstall. In this case I got:

INTERNALERROR>   File "C:\Users\Oscar\miniconda3\lib\site-packages\pytestqt\plugin.py", line 230, in pytest_configure
INTERNALERROR>     qt_api.set_qt_api(config.getini("qt_api"))
INTERNALERROR>   File "C:\Users\Oscar\miniconda3\lib\site-packages\pytestqt\qt_compat.py", line 86, in set_qt_api
INTERNALERROR>     or self._guess_qt_api()
INTERNALERROR>   File "C:\Users\Oscar\miniconda3\lib\site-packages\pytestqt\qt_compat.py", line 77, in _guess_qt_api
INTERNALERROR>     if _can_import(f"{backend}.QtCore"):
INTERNALERROR>   File "C:\Users\Oscar\miniconda3\lib\site-packages\pytestqt\qt_compat.py", line 68, in _can_import
INTERNALERROR>     _import(name)
INTERNALERROR>   File "C:\Users\Oscar\miniconda3\lib\site-packages\pytestqt\qt_compat.py", line 30, in _import
INTERNALERROR>     return __import__(name)
INTERNALERROR> ImportError: DLL load failed while importing QtCore: The specified module could not be found.

prohibiting any test runs unless I set PYTEST_QT_API.

It may be a good idea (or not) to check for ImportError here to avoid this problem: https://github.com/pytest-dev/pytest-qt/blob/c78d21c6c5404f73ef7bfa82ad4806a095c5e1bc/src/pytestqt/qt_compat.py#L70

If you think it is a good idea, I can prepare a patch. (Although I have no idea how to test it, apart from the indication if it runs on my computer with the broken uninstall...)

(Windows 11, I got some error when I installed PyQt6, but has since installed and reinstalled again, this time without any error, still the above happens.)

nicoddemus commented 1 year ago

Yeah not sure if it is a good idea either, because this will just hide the problem entirely... in this case I think it is reasonable to scale the problem up so the user can do something about it.

The-Compiler commented 1 year ago

Agreed. A ModuleNotFoundError is expected in normal day-to-day usage, but any other ImportError is not, and we shouldn't hide it from the user - a loud failure is better than a silent one.

It also looks like this was changed away from ImportError back when dropping Python 2: https://github.com/pytest-dev/pytest-qt/commit/2fc8f680d927b5eeb2de7a66628e7806a06a58f4#diff-04278337b26277c6d10e922eba6f555e9883405df1485c40ead631156499d7b3

oscargus commented 1 year ago

Alright! I see your point and cannot really disagree with it. (Although it would be more "convenient" for me to catch it...) Will close!