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

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

我在安装了 python 3.6 的 Linux mint 19 上创建了我的项目。我将 pytestpytest-qt 结合使用来测试我的应用程序,它是一个 PyQt5 程序。

然后,我已经更新到 Linux mint 20。但是 python 3.8 安装在那里,而不是 3.6。我不知道它是否相关,但现在我不能再 运行 我的测试套件了,当我在终端中键入 pytest 时,它会给我以下输出:

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'

最后它说 ModuleNotFoundError: No module named 'PyQt4' 但我什至不使用 PyQt4,而是使用 PyQt5。

我安装了 pytestpytest-qt。程序本身运行正常。

谢谢。

这部分错误信息:

  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'

提示问题 - 使用的 qt-pytest 版本仍然引用 PyQt4,而 current version 已在一段时间前删除了这些引用 - 意味着安装的 python-qt版本已过时。可以使用

更新版本
pip3 install -U pytest-qt

如评论中所述。目前还不清楚为什么首先要安装旧版本 - 一种可能是旧版本已经被其他软件包安装,通过 pip 安装它(没有 update 选项)会在这种情况下什么都不做。