opencv-python: Breaking changes in module loading v4.6.0.66

Expected behaviour

With opencv-python-headless-4.5.5.64 I was able to import opencv module into Python alike:

from cv2 import (
     cv2
)

this way any related functionalities could be accessed, for example via cv2.putText.

Actual behavior

Since yesterday, opencv-python-headless-4.6.0.66 gets pulled. Any automated build pipelines now break with the error

$ pytest --cov=lib tests/ -v
ImportError while loading conftest '/home/gitlab-runner/builds/QWycMLvj/0/ulb/ulb-it-migration/tests/conftest.py'.
tests/conftest.py:15: in <module>
    from cv2 import (
E   ImportError: Bindings generation error. Submodule name should always start with a parent module name. Parent name: cv2.cv2. Submodule name: cv2

Steps to reproduce

  • example code: please see above
  • ubuntu 20.04 LTS
  • x86
  • opencv-python-headless-4.6.0.66
Issue submission checklist
  • This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 13
  • Comments: 19 (2 by maintainers)

Most upvoted comments

Another pycharm user has to pin the version to 4.5.5.64

Good day, why do you try to import the internal implementation module instead of the using cv2 module directly: import cv2?

$ python3 --version
Python 3.9.12
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install opencv-python-headless==4.6.0.66
$ python -c "import cv2; print(cv2.__version__)"
4.6.0
$ python -c "import cv2; print(cv2.putText)"
<built-in function putText>

it is the same thing to do the following: from csv import _csv internal implementation - it is not possible and raises the error.

please help me, how can fix this in jupyter import cv2 output: ~\anaconda3\lib\site-packages\cv2_init_.py in <module> 179 180 –> 181 bootstrap()

~\anaconda3\lib\site-packages\cv2_init_.py in bootstrap() 173 174 for submodule in __collect_extra_submodules(DEBUG): –> 175 if __load_extra_py_code_for_module(“cv2”, submodule, DEBUG): 176 if DEBUG: print(“Extra Python code for”, submodule, “is loaded”) 177

~\anaconda3\lib\site-packages\cv2_init_.py in __load_extra_py_code_for_module(base, name, enable_debug_print) 26 native_module = sys.modules.pop(module_name, None) 27 try: —> 28 py_module = importlib.import_module(module_name) 29 except ImportError as err: 30 if enable_debug_print:

~\anaconda3\lib\importlib_init_.py in import_module(name, package) 125 break 126 level += 1 –> 127 return _bootstrap._gcd_import(name[level:], package, level) 128 129

~\anaconda3\lib\site-packages\cv2\typing_init_.py in <module> 67 68 if numpy.lib.NumpyVersion(numpy.version) > “1.20.0” and sys.version_info >= (3, 9): —> 69 NumPyArrayGeneric = numpy.ndarray[typing.Any, numpy.dtype[numpy.generic]] 70 else: 71 NumPyArrayGeneric = numpy.ndarray

TypeError: ‘numpy._DTypeMeta’ object is not subscriptable

how it supposed to be imported correctly?

I believe one week is enough to find OpenCV Python samples code:

to see import cv2 as cv everywhere.

It this doesn’t work, then there is a problem with used Python environment.


Besides of strange approach of hacking…

sys.path.insert(0, str(path))

You should not modify the first entry of sys.path as it is reserved.


If you want just to import cv2.so / cv2.pyd / etc, then just put that file into site-packages location directly and drop “other” modules with cv2 name (including egg-like files). But this is not equal to opencv-python package anymore as some parts of OpenCV Python binding are missing in .so file.

The best way to fix autocomplete for a compiled package is to distribute a pyi type stubs file with the package. Then autocomplete could work on just cv2 instead of using the hack that you used previously.

It’s possible that type stubs could be generated automatically by mypy’s stubgen module.

I was able to import opencv module into Python alike:

from cv2 import (
     cv2
)

Just invalid usage. Agreed with @crackwitz

So initial request and this issue are “invalid”.


@banderlog Your example is not correct as you can’t do that for any(!) python module. There are no such obligations on the internal structure from OpenCV Python Bindings which you assumed.

crossposts:

this “from cv2 import cv2” nonsense is wrong. only pycharm users do it. stop doing it. it’s wrong.

This problem is still valid for opencv 4.7.0 if build it from source

I have a folder cv2 with cv2.so in it

If I call import cv2 within the folder – all ok

If I call from without from cv2 import cv2 nothing will work with above described error

There is activity on type stubs generation for OpenCV, but it’s frozen today: they guys are very busy with main job: https://github.com/opencv/opencv/pull/20370