Building on Windows, getting ImportError: DLL load failed while importing cv2

I have a “similar”, but not same problem. I have compiled OpenCV 4.10.0 with:

  • Visual Studio 2022
  • CUDA 12.5.0
  • CuDNN 9.1.1
  • Contrib 4.X
  • Python 3.12.3
    Compilation was succesfull, all DLL are generated, and also a cv2 folder in site-package of Python 3.12 instalation has been generated.
    There are a config-3.11.py file (I have copied also renamed to 3.12.py to try to resolve the issue). Also edited the file and changed python-3.11 to python-3.12.
    Also duplicated the python-3.11 folder with 3.12, both with the file cv2.cp312-win_amd64.pyd inside.
    But in any case, I can’t import cv2 on python with this error:

import cv2 as cv
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2_init_.py”, line 181, in
bootstrap()
File “C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2_init_.py”, line 153, in bootstrap
native_module = importlib.import_module(“cv2”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\AVIAitex\AppData\Local\Programs\Python\Python312\Lib\importlib_init_.py”, line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing cv2: No se puede encontrar el módulo especificado.

I don’t know with DLL file is missing, the opencv_world4100.dll are on path, and OpenCV C++ compiled samples work withyout problem (also tried to copy them to the site-packages folder), but always with this problem.

Anybody knows what is the problem?
Thanks in advance.

Ruben

I post in new thread, as the other is a little old and not the same issue:
I have compiled OpenCV 4.10.0 to use with C++ and Python under Windows with:

  • Visual Studio 2022
  • CUDA 12.5.0
  • CuDNN 9.1.1
  • Contrib 4.X
  • Python 3.12.3
    Compilation was succesfull, all DLL are generated, and also a cv2 folder in site-package of Python 3.12 instalation has been generated.
    There are a config-3.11.py file (I have copied also renamed to 3.12.py to try to resolve the issue). Also edited the file and changed python-3.11 to python-3.12.
    Also duplicated the python-3.11 folder with 3.12, both with the file cv2.cp312-win_amd64.pyd inside.
    But in any case, I can’t import cv2 on python with this error:

import cv2 as cv
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2_init.py”, line 181, in
bootstrap()
File “C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2
init.py”, line 153, in bootstrap
native_module = importlib.import_module(“cv2”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\AVIAitex\AppData\Local\Programs\Python\Python312\Lib\importlib
init_.py”, line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing cv2: No se puede encontrar el módulo especificado.

I don’t know with DLL file is missing, the opencv_world4100.dll are on path, and OpenCV C++ compiled samples work withyout problem (also tried to copy them to the site-packages folder), but always with this problem.

Anybody knows what is the problem?
Thanks in advance.

Ruben

The long answer is that you need to locate the missing dll using process monitor see

That said if your config.py file doesn’t have the line adding the location of the CUDA toolkit? i.e.

os.path.join(os.getenv('CUDA_PATH', 'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.5'), 'bin')

adding that will probably solve your problem.

If you remove/rename the cv2 folder and then build the install again (this should only copy the install files) do you still get files/folders referencing python 3.11 when you are building against 3.12? Also what is your CMake python output? e.g.

  Python 3:
    Interpreter:                 C:/Users/b/mambaforge//python.exe (ver 3.9.16)
    Libraries:                   C:/Users/b/mambaforge//libs/python39.lib (ver 3.9.16)
    numpy:                       C:/Users/b/mambaforge//lib/site-packages/numpy/core/include (ver 1.23.5)
    install path:                C:/Users/b/mambaforge//Lib/site-packages//cv2/python-3.9
2 Likes

Well, thanks in advance for the response. I am trying to fix using your help.
The path on config.py is correct, and I have to use the process monitor to see what is happening. The missing dll is Python311.dll!
The CMake Python output looks strange:

>   Python 3:
>     Interpreter:                 C:/Users/AVIAitex/AppData/Local/Programs/Python/Python312/python.exe (ver 3.11.3)
>     Libraries:                   C:/Users/AVIAitex/AppData/Local/Programs/Python/Python311/libs/python311.lib (ver 3.11.3)
>     Limited API:                 NO
>     numpy:                       C:/Users/AVIAitex/AppData/Local/Programs/Python/Python312/Lib/site-packages/numpy/core/include (ver 1.24.1)
>     install path:                C:/Users/AVIAitex/AppData/Local/Programs/Python/Python312/Lib/site-packages/cv2/python-3.11

But the configuration looks ok:

I have also installed Python 3.11, but not any reference to it, and removed from PATH, so I can’t imagine why it points to Python 3.11 at this point. Any suggestion?

Thanks in advance.
Ruben
PD: I have just completly uninstalled 3.11, and go to rebuild OpenCV to see what happens now.

I would guess something has gone wrong during CMake configuration, maybe you changed a parameter and then configured again and it didn’t work. Definitely the libraries path is wrong and is the reason for your issue.

I would always advise using the command line for CMake and not the GUI for OpenCV because

  1. You can provide the input args when asking a question
  2. It doesn’t suffer as badly from re-configuration issues.

Either way you need to clean your build directory and start again.

See

for details of the command line arguments to use.

Well, thanks so much @cudawarped

Finally I managed it working. I have totally uninstalled Python 3.11 and deleted directory, and begin from 0 the Build (clearing cache on CMake GUI doesn’t work, issues with 3.11 still apears, and I can’t manage CMake without GUI).

After that I still get error on import cv2, but using the process monitor I found that now was missing cudnn64_9.dll (although it was on PATH). So with your tip adding to config.py, now all works correctly.

Thanks again.
Ruben

1 Like