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:DevelopmentToolsOpenCVinstall" -DOPENCV_EXTRA_MODULES_PATH="C:DevelopmentToolsOpenCVopencv_contrib-4.7.0modules" -DBUILD_opencv_world=ON -DBUILD_opencv_python3=ON -DWITH_CUDA=ON -DCUDA_TOOLKIT_ROOT_DIR="C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.8" -DCUDA_ARCH_BIN=8.6 -DWITH_CUDNN=ON -DCUDNN_INCLUDE_DIR="C:Program FilesNVIDIACUDNNv8.9.7include" -DCUDNN_LIBRARY="C:Program FilesNVIDIACUDNNv8.9.7libx64cudnn.lib" -DOpenCV_DNN_CUDA=ON "C:DevelopmentToolsOpenCVopencv-4.7.0"
File Paths:
OpenCV Installation Path: C:DevelopmentToolsOpenCVinstall
DLL Directory: C:DevelopmentToolsOpenCVinstallx64vc16bin
This directory contains opencv_world470.dll
and opencv_videoio_ffmpeg470_64.dll
among others.
Environment Variables:
PATH: Included C:DevelopmentToolsOpenCVinstallx64vc16bin
LIB: Included C:DevelopmentToolsOpenCVinstallx64vc16lib
INCLUDE: Included C:DevelopmentToolsOpenCVinstallinclude
The Issue:
When I try to import cv2 in Python, I receive the following error:
Traceback (most recent call last):
File "C:UsersEdwarDownloadspyTestpyTest.py", line 3, in <module>
import cv2
File "C:UsersEdwarAppDataLocalProgramsPythonPython310libsite-packagescv2__init__.py", line 181, in <module>
bootstrap()
File "C:UsersEdwarAppDataLocalProgramsPythonPython310libsite-packagescv2__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
File "C:UsersEdwarAppDataLocalProgramsPythonPython310libimportlib__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
coderEH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.