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

**URL:** <https://forum.opencv.org/t/building-on-windows-getting-importerror-dll-load-failed-while-importing-cv2/17858>\
**Category:** Uncategorized\
**Tags:** windows, build\
**Created:** [June 5, 2024, 4:03pm UTC](https://forum.opencv.org/t/building-on-windows-getting-importerror-dll-load-failed-while-importing-cv2/17858 "2024-06-05T16:03:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruben\_Kona](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ruben_kona/32/6224_2.png) [@Ruben\_Kona](https://forum.opencv.org/u/Ruben_Kona)\
**Post date:** [June 5, 2024, 4:03pm UTC](https://forum.opencv.org/t/building-on-windows-getting-importerror-dll-load-failed-while-importing-cv2/17858/1 "2024-06-05T16:03:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ruben\_Kona](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ruben_kona/32/6224_2.png) [@Ruben\_Kona](https://forum.opencv.org/u/Ruben_Kona)\
**Post date:** [June 5, 2024, 4:08pm UTC](https://forum.opencv.org/t/building-on-windows-getting-importerror-dll-load-failed-while-importing-cv2/17858/2 "2024-06-05T16:08:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [June 5, 2024, 7:07pm UTC](https://forum.opencv.org/t/building-on-windows-getting-importerror-dll-load-failed-while-importing-cv2/17858/3 "2024-06-05T19:07:10Z")

</div>

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

> **[ImportError: DLL load failed while importing cv2: The specified module could...](https://www.jamesbowley.co.uk/nbs/ImportError_dll_load_failed_while_importing_cv2.html)**
>
> Definitive guide to fixing the ‘ImportError: DLL load failed while importing cv2: The specified module could not be found.’ error when trying to import the OpenCV python module.

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

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

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

```

---

<div class="post-metadata">

**Author:** ![Ruben\_Kona](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ruben_kona/32/6224_2.png) [@Ruben\_Kona](https://forum.opencv.org/u/Ruben_Kona)\
**Post date:** [June 6, 2024, 7:47am UTC](https://forum.opencv.org/t/building-on-windows-getting-importerror-dll-load-failed-while-importing-cv2/17858/4 "2024-06-06T07:47:28Z")

</div>

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:

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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/fd5462975bd75eceb226da66ce93a1091476cce9.png)

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.

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [June 6, 2024, 10:47am UTC](https://forum.opencv.org/t/building-on-windows-getting-importerror-dll-load-failed-while-importing-cv2/17858/5 "2024-06-06T10:47:46Z")

</div>

> [@Ruben\_Kona](#):
>
> 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?

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

> **[Build OpenCV (including Python) with CUDA on Windows: Comprehensive Guide...](https://www.jamesbowley.co.uk/qmd/opencv_cuda_python_windows.html)**
>
> Guide to building OpenCV (including Python bindings) with CUDA (optionally the Nvidia Video Codec SDK and cuDNN) from within Visual Studio or from the command line using the Ninja build system.

for details of the command line arguments to use.

---

<div class="post-metadata">

**Author:** ![Ruben\_Kona](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ruben_kona/32/6224_2.png) [@Ruben\_Kona](https://forum.opencv.org/u/Ruben_Kona)\
**Post date:** [June 6, 2024, 3:22pm UTC](https://forum.opencv.org/t/building-on-windows-getting-importerror-dll-load-failed-while-importing-cv2/17858/6 "2024-06-06T15:22:55Z")

</div>

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
