question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

qtbot fails in github actions if cv2 is imported

See original GitHub issue

What I encountered

I imported cv2 module in my testing file, and github action fails with similar error in #293 . However, when I lazy-import cv2 by loading it inside function block, the error disappears.

Reproducing the error

I made a repository to reproduce this error : https://github.com/JSS95/pytestqt-githubaction

In commit 976f3af, I imported cv2 in function block and the test does not fail.
In commit f23f189, I imported cv2 at the top of the file and the test fails.

My environment

I am using PyQt5 with pytest-qt. My CI ran on ubuntu, Python 3.7. I followed the troubleshooting guide in the document, and all was well before I import cv2.

Error log

============================= test session starts ==============================
platform linux -- Python 3.7.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
PyQt5 5.15.6 -- Qt runtime 5.15.2 -- Qt compiled 5.15.2
rootdir: /home/runner/work/pytestqt-githubaction/pytestqt-githubaction
plugins: xvfb-2.0.0, qt-4.0.2
collected 1 item

Fatal Python error: Aborted

Current thread 0x00007fb4a1414740 (most recent call first):
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pytestqt/plugin.py", line 41 in qapp
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/fixtures.py", line 932 in call_fixture_func
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/fixtures.py", line 1126 in pytest_fixture_setup
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/fixtures.py", line 1072 in execute
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/fixtures.py", line 687 in _compute_fixture_value
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/fixtures.py", line 601 in _get_active_fixturedef
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/fixtures.py", line 581 in getfixturevalue
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/fixtures.py", line 568 in _fillfixtures
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/python.py", line 1647 in setup
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/runner.py", line 449 in prepare
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/runner.py", line 150 in pytest_runtest_setup
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/runner.py", line 255 in <lambda>
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/runner.py", line 311 in from_call
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/runner.py", line 255 in call_runtest_hook
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/runner.py", line 215 in call_and_report
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/runner.py", line 120 in runtestprotocol
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/runner.py", line 109 in pytest_runtest_protocol
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/main.py", line 348 in pytest_runtestloop
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/main.py", line 323 in _main
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/main.py", line 269 in wrap_session
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/main.py", line 316 in pytest_cmdline_main
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/config/__init__.py", line 163 in main
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/_pytest/config/__init__.py", line 185 in console_main
  File "/opt/hostedtoolcache/Python/3.7.12/x64/bin/pytest", line 8 in <module>
/home/runner/work/_temp/035e6ce0-402d-47c0-9d15-d62f155e6d0d.sh: line 1:  1728 Aborted                 (core dumped) pytest

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
JSS95commented, Nov 25, 2021

Thanks. I found https://github.com/opencv/opencv-python/issues/386, and using opencv-python-headless module resolved this error.

For anyone that might encounter same issue:

  1. The reason why #293 raised error was that the server cannot initialize UI session. (https://github.com/opencv/opencv-python/issues/447) Using pytest-xvfb resolves this.
  2. The reason of this error is that cv2 and PyQt5 conflicts on Qt. Installing opencv-python-headless, which has no binding to Qt, can fix this.
0reactions
eyllanesccommented, Mar 7, 2022

If you still want to use python-opencv and not opencv-python-headless then a workaround is:

import os

from PyQt5.QtCore import QLibraryInfo
# from PySide2.QtCore import QLibraryInfo

import cv2

os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(
    QLibraryInfo.PluginsPath
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

qtbot with PySide2 fails in github actions · Issue #293
hi, Hi the following bug in github actions CI not locally: ... qtbot fails in github actions if cv2 is imported #396.
Read more >
Importing cv2 prevents matplotlib from working #386 - GitHub
here is an example of the code : import matplotlib import matplotlib.pyplot as plt import cv2 plt.plot(range(5)) plt.show().
Read more >
Sign up - GitHub
But during testing on single image i am getting error during show which is ... qtbot fails in github actions if cv2 is...
Read more >
Qt Error on cv2.imshow - Specific to version 4.2.0.32 #290
I am suggesting that we do it automatically when the cv2 module is imported so that users don't have to do it themselves....
Read more >
pytest-qt Documentation
you can use opencv-python-headless instead of full opencv-python. More details can be found in issue #396. 12.3. GitHub Actions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found