Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qtbot fails in github actions if cv2 is imported #396

Closed
JSS95 opened this issue Nov 25, 2021 · 6 comments
Closed

qtbot fails in github actions if cv2 is imported #396

JSS95 opened this issue Nov 25, 2021 · 6 comments

Comments

@JSS95
Copy link
Contributor

JSS95 commented Nov 25, 2021

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
@JSS95 JSS95 changed the title Testing fails in github actions if cv2 is imported qtbot fails in github actions if cv2 is imported Nov 25, 2021
@The-Compiler
Copy link
Member

The-Compiler commented Nov 25, 2021

I forked your repository and replaced the CI by a simple script which just does:

from PyQt5.QtWidgets import QApplicationimport cv2app = QApplication([])

and that fails as well:

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/cv2/qt/plugins" even though it was found.
11

. It looks like import cv2 patches Qt stuff in some weird way so that existing Qt code fails to run. Doesn't seem like pytest-qt has anything to do with it though, so I'm closing this.

@JSS95
Copy link
Contributor Author

JSS95 commented Nov 25, 2021

Thanks. I found opencv/opencv-python#386, and using opencv-python-headless module resolved this error.

For anyone that might encounter same issue:

  1. The reason why qtbot with PySide2 fails in github actions #293 raised error was that the server cannot initialize UI session. (qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/surbhi/.local/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found. opencv/opencv-python#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.

@JSS95
Copy link
Contributor Author

JSS95 commented Nov 25, 2021

@The-Compiler Although this is not related to pytest-qt, I think someone else might fall into same trap. I would like to document this if you think it's ok.

@The-Compiler
Copy link
Member

I guess it wouldn't hurt to add this to the troubleshooting page if you want to. Though you got me curious: How come you use that combination of Qt and cv2 in the tests, but not in your application code?

@JSS95
Copy link
Contributor Author

JSS95 commented Nov 25, 2021

I used them in my application as well. But strangely, nothing went wrong in my Windows local machine when I ran my app and tests.

@eyllanesc
Copy link
Contributor

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
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants