DLL load failed
Issue: A Step-by-Step GuideIf you’re using OpenCV with GPU acceleration (CUDA and cuDNN) and encountering the error ImportError: DLL load failed while importing cv2: The specified module could not be found
, this article will walk you through diagnosing and fixing the issue based on a real-world example.
While setting up OpenCV for Python 3.10 with GPU support, I encountered the following error when trying to import OpenCV (cv2
):
Traceback (most recent call last):
File "test.py", line 3, in <module>
import cv2
File "C:\Python310\lib\site-packages\cv2\__init__.py", line 181, in <module>
bootstrap()
File "C:\Python310\lib\site-packages\cv2\__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
File "C:\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: DLL load failed while importing cv2: The specified module could not be found.
PATH
Environment Variable:
C:\DevelopmentTools\OpenCV\install\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin
C:\Program Files\NVIDIA\CUDNN\v8.9.7\bin
LIB
Environment Variable:
C:\DevelopmentTools\OpenCV\install\x64\vc16\lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\lib\x64
C:\Program Files\NVIDIA\CUDNN\v8.9.7\lib\x64
INCLUDE
Environment Variable:
C:\DevelopmentTools\OpenCV\install\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include
C:\Program Files\NVIDIA\CUDNN\v8.9.7\include
This error is common when OpenCV cannot locate the shared libraries (DLLs) required during its runtime. Specifically:
Python 3.8+ DLL Loading Behavior:
PATH
environment variable for DLLs unless explicitly directed to do so.opencv_world470.dll
, cudart64_118.dll
, and cudnn64_8.dll
) were present in the paths, Python didn’t load them.OpenCV Loader Script:
cv2/__init__.py
) dynamically handles paths for its binaries. However, it depends on either os.add_dll_directory()
(Python 3.8+ feature) or a properly configured PATH
.Here’s the step-by-step process to resolve this issue:
Ensure the following paths are included in your environment variables:
PATH
:
C:\DevelopmentTools\OpenCV\install\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin
C:\Program Files\NVIDIA\CUDNN\v8.9.7\bin
LIB
environment variable:
C:\DevelopmentTools\OpenCV\install\x64\vc16\lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\lib\x64
C:\Program Files\NVIDIA\CUDNN\v8.9.7\lib\x64
INCLUDE
environment variable:
C:\DevelopmentTools\OpenCV\install\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include
C:\Program Files\NVIDIA\CUDNN\v8.9.7\include
os.add_dll_directory()
for Python 3.8+If you’re using Python 3.8 or newer, explicitly add these directories in your Python script before importing OpenCV:
import os
# Add OpenCV, CUDA, and cuDNN binary paths
os.add_dll_directory(r'C:\DevelopmentTools\OpenCV\install\bin')
os.add_dll_directory(r'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin')
os.add_dll_directory(r'C:\Program Files\NVIDIA\CUDNN\v8.9.7\bin')
import cv2
print(cv2.getBuildInformation())
This ensures Python explicitly knows where to find the required DLLs.
Use a tool like Dependencies to analyze the cv2.cp310-win_amd64.pyd
file. This tool shows if any required DLLs are missing. Common missing files include:
cudart64_118.dll
(CUDA runtime)cudnn64_8.dll
(cuDNN library)opencv_world470.dll
(OpenCV core)Ensure these files exist in the specified paths.
Install the latest Visual C++ Redistributable for Visual Studio 2019:
After completing the above steps, test OpenCV with a simple script:
import cv2
print(cv2.__version__)
print(cv2.getBuildInformation())
This issue stemmed from Python’s updated DLL loading behavior and OpenCV’s dependency on dynamically linked libraries. By ensuring that all required DLLs are in the correct paths and explicitly specifying these paths for Python, I was able to resolve the problem.
os.add_dll_directory()
to manage DLL paths.PATH
, LIB
, INCLUDE
) are configured correctly.Feel free to use this guide to resolve similar issues in your setup!