# Custom built opencv 4..7.0 python import problem

**URL:** <https://forum.opencv.org/t/custom-built-opencv-4-7-0-python-import-problem/11469>\
**Category:** Python\
**Tags:** programming\
**Created:** [January 1, 2023, 10:56pm UTC](https://forum.opencv.org/t/custom-built-opencv-4-7-0-python-import-problem/11469 "2023-01-01T22:56:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boris\_Kabakov](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/boris_kabakov/32/6858_2.png) [@Boris\_Kabakov](https://forum.opencv.org/u/Boris_Kabakov)\
**Post date:** [January 1, 2023, 10:56pm UTC](https://forum.opencv.org/t/custom-built-opencv-4-7-0-python-import-problem/11469/1 "2023-01-01T22:56:26Z")

</div>

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:

```auto
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](https://github.com/opencv/opencv-python/issues/676)

Does anyone knows how to solve this import issue?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 2, 2023, 12:52pm UTC](https://forum.opencv.org/t/custom-built-opencv-4-7-0-python-import-problem/11469/2 "2023-01-02T12:52:07Z")

</div>

[crosspost](https://meta.stackoverflow.com/a/266159):

> <https://stackoverflow.com/questions/74977869/custom-built-opencv-4-7-0-python-import-problem>

> [@Boris\_Kabakov](#):
>
> from cv2 import cv2

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.

---

<div class="post-metadata">

**Author:** ![Boris\_Kabakov](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/boris_kabakov/32/6858_2.png) [@Boris\_Kabakov](https://forum.opencv.org/u/Boris_Kabakov)\
**Post date:** [January 2, 2023, 2:53pm UTC](https://forum.opencv.org/t/custom-built-opencv-4-7-0-python-import-problem/11469/3 "2023-01-02T14:53:51Z")

</div>

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:

```auto
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:

```auto
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:

```auto
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?”

---

<div class="post-metadata">

**Author:** ![Boris\_Kabakov](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/boris_kabakov/32/6858_2.png) [@Boris\_Kabakov](https://forum.opencv.org/u/Boris_Kabakov)\
**Post date:** [January 2, 2023, 3:00pm UTC](https://forum.opencv.org/t/custom-built-opencv-4-7-0-python-import-problem/11469/4 "2023-01-02T15:00:51Z")

</div>

With 4.5.5 working solution was

```auto
# __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__ )

```
