1

I've encountered an issue where importing cv2 in Python triggers a DLL load failed error. Here's a detailed overview of my setup:

Installation and Configuration: OpenCV Installation: I built OpenCV from source using CMake and Visual Studio 2019. The build was configured for CUDA support with the following command (executed from the build directory):

cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="C:\DevelopmentTools\OpenCV\install" -DOPENCV_EXTRA_MODULES_PATH="C:\DevelopmentTools\OpenCV\opencv_contrib-4.7.0\modules" -DBUILD_opencv_world=ON -DBUILD_opencv_python3=ON -DWITH_CUDA=ON -DCUDA_TOOLKIT_ROOT_DIR="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" -DCUDA_ARCH_BIN=8.6 -DWITH_CUDNN=ON -DCUDNN_INCLUDE_DIR="C:\Program Files\NVIDIA\CUDNN\v8.9.7\include" -DCUDNN_LIBRARY="C:\Program Files\NVIDIA\CUDNN\v8.9.7\lib\x64\cudnn.lib" -DOpenCV_DNN_CUDA=ON "C:\DevelopmentTools\OpenCV\opencv-4.7.0"

File Paths: OpenCV Installation Path: C:\DevelopmentTools\OpenCV\install DLL Directory: C:\DevelopmentTools\OpenCV\install\x64\vc16\bin This directory contains opencv_world470.dll and opencv_videoio_ffmpeg470_64.dll among others.

Environment Variables: PATH: Included C:\DevelopmentTools\OpenCV\install\x64\vc16\bin

LIB: Included C:\DevelopmentTools\OpenCV\install\x64\vc16\lib

INCLUDE: Included C:\DevelopmentTools\OpenCV\install\include

The Issue: When I try to import cv2 in Python, I receive the following error:

Traceback (most recent call last):
  File "C:\Users\Edwar\Downloads\pyTest\pyTest.py", line 3, in <module>
    import cv2
  File "C:\Users\Edwar\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py", line 181, in <module>
    bootstrap()
  File "C:\Users\Edwar\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
  File "C:\Users\Edwar\AppData\Local\Programs\Python\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.

What I've Tried: Adding DLL Directory at Runtime: I've tried explicitly adding the directory containing the OpenCV DLLs to Python's DLL search path:

import os
os.add_dll_directory("C:\\DevelopmentTools\\OpenCV\\install\\x64\\vc16\\bin")
import cv2

Path Verification: I've checked and confirmed that the PATH environment variable includes the correct directory for OpenCV DLLs.

DLL Presence: Verified that opencv_world470.dll and other required DLLs are present in the installation's bin directory.

Request for Help:

What could be causing this error despite having the correct paths set and the DLLs in place? How can I resolve this DLL load failed issue to successfully import and use OpenCV in Python? Any insights into additional dependencies or configuration settings I might have missed would be appreciated.

If code goes well, version should be printed out

1
  • that's not a new problem. please google it. you'll find plenty of suggestions (with a few of them being useful). -- likely you're missing (in PATH) either some microsoft runtime DLLs and/or some CUDA DLLs (more likely). Commented Dec 25, 2024 at 14:43

1 Answer 1

2

Just found a solution

Resolving "DLL Load Failed While Importing cv2" in Python with OpenCV and CUDA/cuDNN


Problem Description

When attempting to use a custom-built version of OpenCV 4.7.0 with GPU (CUDA and cuDNN) support on Windows, the following error occurred during import:

Traceback (most recent call last):
  File "pyTest.py", line 3, in <module>
    import cv2
  File "C:\Users\Edwar\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py", line 181, in <module>
    bootstrap()
  File "C:\Users\Edwar\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
  File "C:\Users\Edwar\AppData\Local\Programs\Python\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.

Environment Details

Python Configuration

  • Python Version: 3.10
  • OpenCV Version: 4.7.0 (custom build)

Build Configuration

OpenCV was built with GPU and Python support using the following CMake command:

cmake -G "NMake Makefiles" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="C:/DevelopmentTools/OpenCV/install" \
    -DOPENCV_EXTRA_MODULES_PATH="C:/DevelopmentTools/OpenCV/opencv_contrib-4.7.0/modules" \
    -DBUILD_opencv_world=ON \
    -DBUILD_opencv_python3=ON \
    -DWITH_CUDA=ON \
    -DCUDA_TOOLKIT_ROOT_DIR="C:/symblinkNvidia/CUDA" \
    -DCUDA_ARCH_BIN=8.6 \
    -DWITH_CUDNN=ON \
    -DCUDNN_INCLUDE_DIR="C:/symblinkNvidia/CUDNN/include" \
    -DCUDNN_LIBRARY="C:/symblinkNvidia/CUDNN/lib/x64/cudnn.lib" \
    -DOpenCV_DNN_CUDA=ON

Note1: I used symblink for C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\, C:\Program Files\NVIDIA\CUDNN\v8.9.7\include, and C:\Program Files\NVIDIA\CUDNN\v8.9.7\lib\x64\cudnn.lib to avoid white space issue during the nmake process.

Note2: I specified C:/symblinkNvidia/CUDNN/lib/x64/cudnn.lib for -DCUDNN_LIBRARY because If I passed C:/symblinkNvidia/CUDNN/lib/x64/ , x64.lib which does not exist would be considered as a file in C:\DevelopmentTools\OpenCV\build\CMakeCache.txt and this will cause nmake process to crash.

Environment Variables

Here are the relevant environment variables:

  • PATH:

    C:\DevelopmentTools\OpenCV\install\x64\v16\bin
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin
    C:\Program Files\NVIDIA\CUDNN\v8.9.7\bin
    
  • LIB:

    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\lib\x64
    C:\Program Files\NVIDIA\CUDNN\v8.9.7\lib\x64
    
  • INCLUDE:

    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include
    C:\Program Files\NVIDIA\CUDNN\v8.9.7\include
    

Cause of the Problem

The error occurred because Python was unable to locate the required shared libraries (DLLs) for OpenCV, specifically the cuDNN runtime library (cudnn64_8.dll).

  1. Python's DLL Loading Behavior:

    • Starting with Python 3.8, the mechanism for locating DLLs changed. By default, Python no longer searches the PATH environment variable for DLLs. Instead, DLL directories must be explicitly added using the os.add_dll_directory() function.
  2. Missing cuDNN DLLs:

    • OpenCV, when built with CUDA and cuDNN support, relies on cuDNN libraries for deep learning acceleration. In this case, the required cudnn64_8.dll was not found by Python during runtime, leading to the error.
  3. Solution Specificity:

    • Through testing, it was discovered that explicitly adding the cuDNN binary directory to Python’s search paths resolves the issue.

Solution

To resolve the issue, add the cuDNN binary directory to Python’s DLL search paths before importing OpenCV:

import os

# Add cuDNN DLL directory
os.add_dll_directory(r'C:\Program Files\NVIDIA\CUDNN\v8.9.7\bin')

# Import OpenCV
import cv2
print(cv2.__version__)
print(cv2.getBuildInformation())

Testing the Solution

After applying the fix, OpenCV successfully imports, and the following output is observed:

4.7.0

General configuration for OpenCV 4.7.0 =====================================
  Version control:               unknown
  CUDA:                          YES (ver 11.8, CUFFT CUBLAS)
  cuDNN:                         YES (ver 8.9.7)
  Python 3:                      YES (ver 3.10)
  ...

Key Takeaways

  1. Why Only cuDNN Needs to Be Set:

    • While CUDA libraries (cudart64_118.dll) are also required, they are generally found automatically if CUDA is installed and the PATH environment variable is configured. cuDNN, however, must be explicitly specified probably due to how I have set up the cmake command for the C:\DevelpomentTools\OpenCV\build\ folder.
  2. Simplified Setup:

    • Instead of adding all library paths manually, focusing on the cuDNN path is sufficient to resolve this specific error.
  3. Future Builds:

    • When building OpenCV or other projects with GPU acceleration, ensure the following:
      • CUDA and cuDNN versions are compatible.
      • The cuDNN path is explicitly added in Python scripts when using Python 3.8 or later.

Summary

Problem:

The error DLL load failed while importing cv2 occurred because Python could not locate the required cudnn64_8.dll file for OpenCV with CUDA/cuDNN support.

Cause:

This issue was caused by Python 3.8+ no longer searching the PATH environment variable for DLLs, combined with OpenCV's reliance on cuDNN for deep learning operations.

Solution:

Explicitly add the cuDNN binary directory to Python's DLL search paths using:

import os
os.add_dll_directory(r'C:\Program Files\NVIDIA\CUDNN\v8.9.7\bin')

Outcome:

OpenCV successfully imports, and GPU-accelerated features are available.

By following this approach, similar DLL-related errors can be resolved for other projects and dependencies.

Sign up to request clarification or add additional context in comments.

2 Comments

where is all that coming from? did some AI help?
I did tests on my laptop. due to the cmake setup i wrote for the nmake process, only cudnn.lib was written into build list. I only passed cudnn.lib because if I passed the whole cudnn library directory, x64.lib which does not exist would be considered a file in CMakeCache, and this could cause crash in the make or nmake process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.