DLL load failed when importing a self-built cv2 library in python

Hello,

The main issue I’m facing is that when I try to import cv2 that I manually built to the python environment, I received an error saying that the DLL load failed. Allow me to tell you the steps I did prior to this point.


I wanted to use Gstreamer with OpenCV, but since OpenCV doesn’t come native with Gstreamer, I had to build it manually from source. Here’s my current setup:

  • Windows 10 22H2
  • Python 3.12
  • Gstreamer 1.24.0 MinGWx64
  • Cmake GUI 3.29.0
  • OpenCV 4.9.0 downloaded from the official site
  • Visual Studio Community 2022 (17.9.4)

I had this guide as reference for my installation.

After I browsed for the source file and press configure, the CMake GUI did some process and this is what it looked like after the process was done

Notice that the values of some names in the Gstreamer section seems to be missing. Also in the Cmake log (lower part), Gstreamer is marked as NO.


I then added “GSTREAMER_DIR” as an entry via the “Add entry” button, pointing to my Gstreamer installation folder. After re-configuring, all of the values are now filled as like this, and Gstreamer is now marked as YES (1.24.0)

Next, I proceed to Generate, open the project in Visual Studio, then build the source as shown in the guide. The final build had 1 failed component (not sure if it’s right to call it a component, but it showed something like this)

========== Build: 94 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 10:44 PM and took 08:27.743 minutes ==========

There were also a few errors as shown in the picture below (the website seems to block me from posting more than 1 image, so let me post it in the comments instead)

But when I checked inside the build\lib\python3\Release folder, there was a .pyd file in it (cv2.cp312-win_amd64.pyd). I then copied this to my site-packages folder in my python installation folder and tried to import it. But the import didn’t work, and it showed this error, saying that DLL load failed. Runtime was in CMD, by the way.

C:\Users\NICE>python
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.add_dll_directory(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\bin")
<AddedDllDirectory('C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v12.4\\bin')>
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\NICE\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2\__init__.py", line 181, in <module>
    bootstrap()
  File "C:\Users\NICE\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2\__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NICE\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: The specified module could not be found.
>>>

I also tried to use process monitoring to check what DLLs are missing. Upon brief inspection, most DLLs have the result of “NAME NOT FOUND”, but a few lines down, that corresponding DLL is loaded from another directory and the result is “SUCCESS”, so I’m assuming there was no problem for those DLLs. However, there is one DLL file that seems to never get the “SUCCESS” result, and it is the opencv_videoio.dll. I noticed that the project name shown in my Visual Studio error list is also similar to this name, so maybe there’s some correlation between them?


What’s weird is that ‘opencv_videoio.dll’ is inside the build folder. I even tried to manually include the build folder using this code

import os
os.add_dll_directory(path\to\my\build\folder)

I'm really stumped as to what to do next. Can you point me to the right direction?

Some things that I think is relevant:

  1. When I didn’t manually add the missing values and built it with Gstreamer marked as NO, the build was successful and I could import it properly in python by copying the .pyd file to my site-lib directory. So I’m thinking if something is wrong with my Gstreamer installation or something? (I tried installing the MSVC version as well, and it also didn’t build properly. I might say it built worse, failing to build around 40 components instead of just 1)

  2. I also tried installing NVIDIA CUDA Toolkit as a last ditch attempt. I made sure to reset the CMake cache, and repeat the process from scratch, but the error seems to be the same. Similar to the page, I also tried including the NVIDIA CUDA Toolkit installation folder to the dll directory but the error is still not fixed.

The error log shown in Visual Studio can be seen below

gstreamer DLLs, or whatever other DLLs, need to be findable. that often translates into adding those directories containing the DLLs to the PATH env var.

1 Like

I already added the Gstreamer directory, as well as the build folderpath that contains the opencv_videoio.dll to my PATH env var, but it still wont load the DLL file.

I’m fairly certain that the missing DLLs (or maybe just singular DLL, the opencv_videoio.dll?) is not related to the Gstreamer.

I’m wondering if this has anything to do with the partially successful build?

No It does not work like this since python 3.8 (Issue 36085: Enable better DLL resolution - Python tracker)

1 Like

Sorry, but I’m not really sure what you mean by ‘like this’?
The link you gave me seems to be a discussion about making improvements in DLL detection (?), and I don’t see an alternate solution in that link

Since python 3.8 you need to use

os.add_dll_directory(GSTREAMER_BIN_DIR)

because dll’s from directories on your path are no longer loaded automatically.

I tried to add the directory with the function you told me, but it seems the error still persists

import os

# gstreamer bin path
os.add_dll_directory(r"D:\gstreamer\1.0\mingw_x86_64\bin")
# nvidia cuda bin path
os.add_dll_directory(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\bin")
# opencv build path
os.add_dll_directory(r"D:\NICE\Computer Vision\build-opencv-manual\build\bin")

import cv2
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 6
      3 os.add_dll_directory(r"D:\gstreamer\1.0\mingw_x86_64\bin")
      4 os.add_dll_directory(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\bin")
----> 6 import cv2

File c:\Users\NICE\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2\__init__.py:181
    176             if DEBUG: print("Extra Python code for", submodule, "is loaded")
    178     if DEBUG: print('OpenCV loader: DONE')
--> 181 bootstrap()

File c:\Users\NICE\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2\__init__.py:153, in bootstrap()
    149 if DEBUG: print("Relink everything from native cv2 module to cv2 package")
    151 py_module = sys.modules.pop("cv2")
--> 153 native_module = importlib.import_module("cv2")
    155 sys.modules["cv2"] = py_module
    156 setattr(py_module, "_native", native_module)

File c:\Users\NICE\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py:90, in import_module(name, package)
     88             break
     89         level += 1
---> 90 return _bootstrap._gcd_import(name[level:], package, level)

ImportError: DLL load failed while importing cv2: The specified module could not be found.

I also tried using dependency walker to check which .dll files are missing, and it seems that I’m missing a lot of stuff.

I also checked the .pyd file that is built normally (without Gstreamer, without any failed notifications in Visual Studio when building, and is able to be imported just fine to the python environment), and the dependency walker also showed many .dll files are missing.

You can see the comparison in this picture between the .pyd file I built with Gstreamer, vs the normally built .pyd file without GStreamer

Could it be that the 1 component which failed to build caused this issue?

Maybe your missing the VS redistributable or the media pack

The VS redistributable is already installed on my PC. And since my windows is not an N version, I suppose I don’t need to install the media pack.

I tried running the Dependencies program mentioned in your link, and it seems that the missing DLL files that was seen in the Dependency Walker is now found. But there are still some missing DLLS (upon checking, all of those missing .dll files are in the build folder of my opencv). Here’s a picture showing the .dll files in my opencv build folder (left side. the relative path is …\build\bin\release), and the missing .dll files detected with the Dependencies program (right side)

But as you can see in my previous post, I already included the dll directory of my build folder (build\bin) via the os.add_dll_directory(), and the error still persists.

Could it be caused by corrupted DLLs due to the 1 failed build process I mentioned in my initial post?

DO NOT take depwalker results as the word of god.

some of those failed at-runtime lookups come from probing for optional stuff, or variants.

I’ve only ever had issues with OpenCV and DLLs any time I was reckless enough to involve gstreamer in any way. that library brings its own “versions” of msvc runtime libraries, which makes you understand the term “DLL Hell”.

Oh no :frowning:

Seems like I’m better off to look at other alternatives for Gstreamer then?
Initially I want to use openCV because it could streamline the integration for my project. But oh well.

Thank you all for the suggestions!

gstreamer appears tamer on linux. if you’re feeling adventurous, you could look into building it all under WSL/WSL2.

Ah but then it would it work on my Windows environment? Since the environment is so different and all

Process monitor is the only tool you need for this error and its ouput is giving you the correct information. The error implies you didn’t build the opencv_videoio module successfully which is why your getting the error.

When you build the INSTALL target without errors you will have a cv2 folder inside your site-packages directory containg the .pyd so you will need to delete the one you manually copied for everything to work correctly.

Once that is working, if you still have problems then use Process Monitor again. If there are too many entries, export the log File->Save (Comma-Separated Values) and parse it manually e.g.


import csv
from pathlib import Path
import re
def print_missing_libs(process_mon_export_file_path):
    shared_libs = {}
    with open(process_mon_export_file_path, mode='r') as file:
        for row in csv_reader:
            key = Path(row[3]).name
            val = row[4]
            if (key not in shared_libs or key in shared_libs and shared_libs[key] != 'SUCCESS'):
                shared_libs[key] = val
    
    pattern = re.compile(r"__init__")
    for entry, status in shared_libs.items():
        if(status == 'NAME NOT FOUND' and not re.search(pattern, entry)):
            print(entry)

print_missing_libs(Logfile.CSV)

Then deal with the non windows dll’s (nvidia, gstreamer) first as they can be false positives.

1 Like