-
Notifications
You must be signed in to change notification settings - Fork 858
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
Package conflict between opencv-python and opencv-python-headless #467
Comments
It's not possible to separate the Qt libraries into another package because OpenCV is compiled against them. They must be present or the dynamic linking will fail during runtime. It would be of course great to have that kind of extra packages (or just a single package, and the user could then install the extra dependencies via some other package manager), but it's not simply possible with the current OpenCV C++ side architecture. It would require that the whole C++ side is refactored to load 3rd party dependencies during runtime instead of making them hard dependencies during compile time. For example, on the Windows side, FFmpeg is loaded during runtime like this, and not having it next to the binary is not an issue. The reason why PyQt5 conflicts with the package is that we ship with a different version of the Qt 5 platform plugin than PyQt5 and they are not ABI compatible. This is the line that breaks PyQt5: https://github.com/opencv/opencv-python/blob/master/cv2/__init__.py#L23 I think this is one of the cleanest solutions that could be used to circumvent the issue: #386 (comment) It requires some testing to confirm that it works. Another option is to use different backend with The error you are seeing is coming from |
The unsatisfying but ultimately stable solution I've gone with is to hack my setup file with extra requirements:
The requirements directory: https://gitlab.kitware.com/computer-vision/kwcoco/-/tree/main/requirements In headless.txt I have:
and in graphics.txt I have:
Then I have a check at import time in import ubelt as ub
try:
import cv2 # NOQA
except ImportError as ex:
msg = ub.paragraph(
'''
The kwimage module failed to import the cv2 module. This may be due to
an issue https://github.com/opencv/opencv-python/issues/467 which
prevents us from marking cv2 as package dependency.
To work around this we require that the user install this package with
one of the following extras tags:
`pip install kwimage[graphics]` xor
`pip install kwimage[headless]`.
Alternatively, the user can directly install the cv2 package as a post
processing step via:
`pip install opencv-python-headless` or
`pip install opencv-python`.
We appologize for this issue and hope this documentation is sufficient.
orig_ex={!r}
''').format(ex)
raise ImportError(msg) So the user has to choose And FWIW, I have a bash script that "ensures" the system has the headless version of opencv installed: fix_opencv_conflicts(){
__doc__="
Check to see if the wrong opencv is installed, and perform steps to clean
up the incorrect libraries and install the desired (headless) ones.
"
# Fix opencv issues
pip freeze | grep "opencv-python=="
HAS_OPENCV_RETCODE="$?"
pip freeze | grep "opencv-python-headless=="
HAS_OPENCV_HEADLESS_RETCODE="$?"
# VAR == 0 means we have it
if [[ "$HAS_OPENCV_HEADLESS_RETCODE" == "0" ]]; then
if [[ "$HAS_OPENCV_RETCODE" == "0" ]]; then
pip uninstall opencv-python opencv-python-headless -y
pip install opencv-python-headless
fi
else
if [[ "$HAS_OPENCV_RETCODE" == "0" ]]; then
pip uninstall opencv-python -y
fi
pip install opencv-python-headless
fi
}
Hope this helps someone. And if anyone has an improvement to this method, let me know. |
Preface: This is somewhat of a weird issue because I can't manage reproduce it with a MWE (perhaps someone here will be able to shed light on why).
I have a library: kwimage that depends on the
cv2
module, and there are two main ways of obtaining this with pip. Eitheropencv-python
oropencv-python-headless
. Currently the install_requires simply specified "opencv-python".The issue is that
opencv-python
contains libraries that can conflict withpyqt5
, which I often see when I'm usingmatplotlib
. Usingopencv-python-headless
works around this issue.The problem is that pip has gotten too smart for its own good. If I uninstall opencv-python and install opencv-python-headless, it throws some requirements not satisfied error, but I can't figure out how to reproduce that reliably (in that I can't set up another package where it lists some package as a dependency and then remove it to reproduce the error). However, when it does happen it looks like this:
I'm currently going to work around this by making both
opencv-python
andopencv-python-headless
optional installs in the install_extras section with different tags. This will solve 80% of the issue, but I think a modification to how these opencv wheels are produced might 100% solve the issue.What I'm wondering is if it is possible for there to be an
opencv-python-base
package, which is effectively the headless code and then a plugin packageopencv-python-graphics
could be installed on top of that to only add the qt-libs? Thus theopencv-python
package could maintain backwards compatibility by simply being a meta package that requiresopencv-python-base
andopencv-python-graphics
.I'm not sure if this is easy / possible to do, because perhaps the "non-headless" library has some compile time difference that can't be augmented at runtime.
If anyone can shed more light on the pkg_resources issue or comment on the feasibility of the "graphics-libs-as-a-plugin-module" idea, that would be appreciated.
The text was updated successfully, but these errors were encountered: