Repository: opencv
Issue: Python bindings cv2 recursion issue
Labels: bug,question (invalid tracker),category: python bindings,category: build/install

### System Information

OpenCV python version: 4.10
Operating System / Platform: Debian GNU/Linux 12 (bookworm) aarch64 (running on Raspberry Pi 4B)
Python Version: 3.9.19

### Detailed description

After several compilations, everytime I try to do an import of the cv2 module, I get the following error:
```
 python -c "import cv2"
['', '/opt/intel/openvino_2022.3.2/python/python3.9', '/opt/intel/openvino_2022.3.2/python/python3', '/home/oferrer/.pyenv/versions/3.9.19/envs/opencv_3.9/lib/python3.9/site-packages/cv2', '/home/oferrer/.pyenv/versions/3.9.19/lib/python39.zip', '/home/oferrer/.pyenv/versions/3.9.19/lib/python3.9', '/home/oferrer/.pyenv/versions/3.9.19/lib/python3.9/lib-dynload', '/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages']
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
  File "/home/oferrer/.pyenv/versions/3.9.19/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/__init__.py", line 76, in bootstrap
    raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
```

### Steps to reproduce

- Setup and activate python environment
``` 
pyenv virtualenv 3.9 opencv_3.9
pyenv activate opencv_3.9
```
- Setup openvino 2022.3.2 (need Intel NCS2 support) from repository (this includes downloading the code form arm64, installing dependencies and sourcing the setupenvars.sh script to get env variables in place). 
- Download latest opencv version
```
git clone --recurse-submodules https://github.com/opencv/opencv.git
```
- Create parallel build directory
```
mkdir build_opencv
``` 
- Run Cmake with options to support the downloaded openvino and add the python bindings
```
cmake \
-D BUILD_INFO_SKIP_EXTRA_MODULES=ON \
-D BUILD_EXAMPLES=OFF \
-D BUILD_JASPER=OFF \
-D BUILD_JAVA=OFF \
-D BUILD_JPEG=ON \
-D BUILD_APPS_LIST=version \
-D BUILD_opencv_apps=ON \
-D BUILD_opencv_java=OFF \
-D BUILD_OPENEXR=OFF \
-D BUILD_PNG=ON \
-D BUILD_TBB=OFF \
-D BUILD_WEBP=OFF \
-D BUILD_ZLIB=ON \
-D WITH_1394=OFF \
-D WITH_CUDA=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GPHOTO2=OFF \
-D WITH_GSTREAMER=ON \
-D OPENCV_GAPI_GSTREAMER=ON \
-D WITH_GTK_2_X=OFF \
-D WITH_IPP=OFF \
-D WITH_JASPER=OFF \
-D WITH_LAPACK=OFF \
-D WITH_MATLAB=OFF \
-D WITH_MFX=OFF \
-D WITH_OPENCLAMDBLAS=OFF \
-D WITH_OPENCLAMDFFT=OFF \
-D WITH_OPENEXR=OFF \
-D WITH_OPENJPEG=OFF \
-D WITH_QUIRC=OFF \
-D WITH_TBB=OFF \
-D WITH_TIFF=OFF \
-D WITH_VTK=OFF \
-D WITH_WEBP=OFF \
-D CMAKE_USE_RELATIVE_PATHS=ON \
-D CMAKE_SKIP_INSTALL_RPATH=ON \
-D ENABLE_BUILD_HARDENING=ON \
-D ENABLE_CONFIG_VERIFICATION=ON \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D ENABLE_CXX11=ON \
-D INSTALL_PDB=ON \
-D INSTALL_TESTS=ON \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D CMAKE_INSTALL_PREFIX=install \
-D OPENCV_SKIP_PKGCONFIG_GENERATION=ON \
-D OPENCV_SKIP_PYTHON_LOADER=OFF \
-D OPENCV_SKIP_CMAKE_ROOT_CONFIG=ON \
-D OPENCV_GENERATE_SETUPVARS=OFF \
-D OPENCV_BIN_INSTALL_PATH=bin \
-D OPENCV_INCLUDE_INSTALL_PATH=include \
-D OPENCV_LIB_INSTALL_PATH=lib \
-D OPENCV_CONFIG_INSTALL_PATH=cmake \
-D OPENCV_3P_LIB_INSTALL_PATH=3rdparty \
-D OPENCV_SAMPLES_SRC_INSTALL_PATH=samples \
-D OPENCV_DOC_INSTALL_PATH=doc \
-D OPENCV_OTHER_INSTALL_PATH=etc \
-D OPENCV_LICENSES_INSTALL_PATH=etc/licenses \
-D OPENCV_INSTALL_FFMPEG_DOWNLOAD_SCRIPT=ON \
-D BUILD_opencv_world=OFF \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_PACKAGES_PATH=install/python/python3 \
-D PYTHON3_LIMITED_API=ON \
-D HIGHGUI_PLUGIN_LIST=all \
-D OPENCV_PYTHON_INSTALL_PATH=python \
-D CPU_BASELINE=NEON \
-D OPENCV_IPP_GAUSSIAN_BLUR=OFF \
-D WITH_OPENVINO=ON \
-D VIDEOIO_PLUGIN_LIST=gstreamer \
-D CMAKE_EXE_LINKER_FLAGS=-Wl,--allow-shlib-undefined \
-D CMAKE_BUILD_TYPE=Release ../opencv
``` 
- Make and Install
```
make -j $(nproc) # to use all the rpi4 cores
cmake -P cmake_install.cmake
``` 
- Install bindings in the virtual environment
``` 
pip install ./python_loader
```
- Touch a config-3.9.py file as appears required, but there's no reference in the documentation.
```
touch ~/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/config-3.9.py
```
- Attempt to import the module in a python script (with output)
```
python -c "import cv2"
['', '/opt/intel/openvino_2022.3.2/python/python3.9', '/opt/intel/openvino_2022.3.2/python/python3', '/home/oferrer/.pyenv/versions/3.9.19/envs/opencv_3.9/lib/python3.9/site-packages/cv2', '/home/oferrer/.pyenv/versions/3.9.19/lib/python39.zip', '/home/oferrer/.pyenv/versions/3.9.19/lib/python3.9', '/home/oferrer/.pyenv/versions/3.9.19/lib/python3.9/lib-dynload', '/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages']
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
  File "/home/oferrer/.pyenv/versions/3.9.19/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/oferrer/.pyenv/versions/opencv_3.9/lib/python3.9/site-packages/cv2/__init__.py", line 76, in bootstrap
    raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
```


### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)