Search code examples
pythondarknet

Import darknet FileNotFoundError: Could not find module


I am getting this error when I run import darknet:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\darknet-master\build\darknet\x64\darknet.py", line 211, in <module>
    lib = CDLL(winGPUdll, RTLD_GLOBAL)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'C:\Users\darknet-master\build\darknet\x64\yolo_cpp_dll.dll' (or one of its dependencies). Try using the full path with constructor syntax.```

Solution

  • Instead of just downloading yet another version of python (yawn), we can fix darknet.py to import the DLLs correctly. As mentioned here, we need to add correct DLL import paths if using python > 3.8. The solution is to add these lines

    os.add_dll_directory('c:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin')
    os.add_dll_directory(os.path.dirname(__file__))
    

    somewhere before the CDLL calls. You can place them at the top of the script.

    The first line allows python to load DLLs from your CUDA install which you need if you're using GPU.

    The second line allows python to load DLLs from the current working directory (into which you should copy yolo_cpp_dll.dll and pthreadVC2.dll). Alternatively you could replace this to the path that contains these DLLs.