pythonpyqt5pytestpython-3.8pytest-qt

Python 3.8 pytest-qt ModuleNotFoundError: No module named 'PyQt4'


I have created my project on Linux mint 19 with python 3.6 installed. I used pytest in conjunction with pytest-qt to test my app which is a PyQt5 program.

Then, I've updated to Linux mint 20. However python 3.8 is installed there instead of 3.6. I don't know whether it's related or not but now I can't run my test suite anymore, when I type pytest in terminal it gives me the following output:

Traceback (most recent call last):
  File "/home/kotlin/.local/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 187, in console_main
    code = main()
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 143, in main
    config = _prepareconfig(args, plugins)
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 318, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "/home/kotlin/.local/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/home/kotlin/.local/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/home/kotlin/.local/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  File "/home/kotlin/.local/lib/python3.8/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/helpconfig.py", line 100, in pytest_cmdline_parse
    config = outcome.get_result()  # type: Config
  File "/home/kotlin/.local/lib/python3.8/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/home/kotlin/.local/lib/python3.8/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 1003, in pytest_cmdline_parse
    self.parse(args)
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 1280, in parse
    self._preparse(args, addopts=addopts)
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 1172, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "/home/kotlin/.local/lib/python3.8/site-packages/pluggy/manager.py", line 299, in load_setuptools_entrypoints
    plugin = ep.load()
  File "/usr/lib/python3.8/importlib/metadata.py", line 77, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/assertion/rewrite.py", line 171, in exec_module
    exec(co, module.__dict__)
  File "/home/kotlin/.local/lib/python3.8/site-packages/pytestqt/plugin.py", line 7, in <module>
    from pytestqt.qt_compat import QtCore, QtGui, QtTest
  File "/home/kotlin/.local/lib/python3.8/site-packages/_pytest/assertion/rewrite.py", line 171, in exec_module
    exec(co, module.__dict__)
  File "/home/kotlin/.local/lib/python3.8/site-packages/pytestqt/qt_compat.py", line 31, in <module>
    import PyQt4.QtCore as _QtCore
ModuleNotFoundError: No module named 'PyQt4'

At the end it says ModuleNotFoundError: No module named 'PyQt4' but I don't even use PyQt4, I use PyQt5 instead.

I have pytest and pytest-qt both installed. The program itself works properly.

Thanks.


Solution

  • This part of the error message:

      File "/home/kotlin/.local/lib/python3.8/site-packages/pytestqt/qt_compat.py", line 31, in <module>
        import PyQt4.QtCore as _QtCore
    ModuleNotFoundError: No module named 'PyQt4'
    

    hints at the problem - the used qt-pytest version still references PyQt4, while the current version has these references removed some time ago -- meaning the installed python-qt version is outdated. The version can be updated using

    pip3 install -U pytest-qt
    

    as mentioned in the comments. It is still unclear why an old version was installed in the first place - one possibility is that an old version already had been installed by some other package, installing it via pip (without the update option) would not do anything in this case.