Custom built opencv 4..7.0 python import problem

I built opencv 4.7.0 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 or similar stuff, nothing will work with error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: Bindings generation error. Submodule name should always start with a parent module name. Parent name: cv2.cv2. Submodule name: cv2

Everything worked with opencv 4.5.5

I’ve seen similar problems with opencv-python: Breaking changes in module loading v4.6.0.66 · Issue #676 · opencv/opencv-python · GitHub

Does anyone knows how to solve this import issue?

crosspost:

that doesn’t work because it’s wrong. it’s supposed to fail.

when you’ve built OpenCV, you have to install it properly. that goes for the python bindings too.

pycharm continues to be a menace to (OpenCV) society.

You do recognize, that your answer is not actually an answer, didnt you?

I do not use pycharm, I run python prompt from linux terminal. And that statement “that doesn’t work because it’s wrong” makes me fill suspicious about your competence.

Let’s take a simple example with some dumb python module with only one function:

bkaba@bkaba:~$ tree some_python_module/
some_python_module/
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-310.pyc
│   └── some_python_module.cpython-310.pyc
└── some_python_module.py

Contents of module:

bkaba@bkaba:~$ cat some_python_module/some_python_module.py
def foo():
    print('Oh no, it works')

Now let’s run it with the same python import rules:

bkaba@bkaba:~$ python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from some_python_module import some_python_module
>>> some_python_module.foo()
Oh no, it works

Now, please, tell me, why it supposed to fail and why it worked with opencv 4.5.5?

But my original question was “how I supposed to import custom built opencv lib in python module?”

With 4.5.5 working solution was

# __init__.py
import importlib
from .cv2 import *

# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module('cv2.cv2').__dict__)