hmeine / qimage2ndarray

qimage2ndarray is a small python extension for quickly converting between QImages and numpy.ndarrays (in both directions).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[bug] QtDriver.detect_qt is not finding the driver

rodrigomologni opened this issue · comments

Hi developers!

The package is not working without PyQt5:

Traceback (most recent call last):
  File "D:\Users\Rodrigo\Desktop\temp.py", line 1, in <module>
    import qimage2ndarray
  File "d:\users\rodrigo\github\qimage2ndarray\qimage2ndarray\__init__.py", line 4, in <module>
    from .dynqt import QtGui as _qt
  File "d:\users\rodrigo\github\qimage2ndarray\qimage2ndarray\dynqt.py", line 4, in <module>
    QtGui = qt.QtGui
  File "d:\users\rodrigo\github\qimage2ndarray\qimage2ndarray\qt_driver.py", line 131, in __getattr__
    return self.importMod(name)
  File "d:\users\rodrigo\github\qimage2ndarray\qimage2ndarray\qt_driver.py", line 126, in importMod
    qt = __import__('%s.%s' % (self._drv, mod))
ModuleNotFoundError: No module named 'PyQt5'
[Finished in 369ms]

The error occurs because the method QtDriver.detect_qt is not finding the driver. It always returns None.

detect_qt uses sys.modules. This property maps only modules which have already been loaded. An alternative is:

def detect_qt():
    for qt in ["PyQt6", "PySide6", "PyQt5", "PySide2"]:
        try:
            exec(f"import {qt}")
        except ModuleNotFoundError:
            pass
        else:
            return qt

True; one is expected to set the QT_API environment variable to select the driver. The detection of already imported modules should give a better default when qimage2ndarray is used in application that already use one of the Qt wrappers. Otherwise, the global default will kick in, and… maybe that's a bad idea in the first place.

Anyhow, I would even more dislike the idea of just trying out imports; these not only take resources, but would also lead to surprising (or even dangerous) behavior if the wrong package is found.