'DLL load failed, module not found?

I have a folder with a file "main.py" and a file "_test.pyd" (note .pyd). The file "main.py" looks like this:

import _test

I get the following error:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import _test
ImportError: DLL load failed while importing _test: The specified module could not be found.

Why is this error coming up? Cheers.

Note: I was given this code by others, and it works for the original authors, so I'm not sure what's wrong with me/my machine.

Update: Running os.path.isfile('_test.pyd') returns True, so I don't think it's a problem with the path



Solution 1:[1]

You should append the path of the folder which contains the imported module before import.

Code:

import os
import sys

sys.path.append(os.path.realpath(os.path.dirname(__file__)))

import _test  # noqa: E402

EDIT:

Other ideas:

  • Adding __init__.py file to the related director.
  • Checking the PyInit_foo() function in .pyd file.
    • If the Python finds the .pyd file, it will attempt to call PyInit_foo() to initialize it

Solution 2:[2]

Update Following posts from people experiencing similar issues, I tried downgrading my Python version (from 3.8.4rc1 to 3.5.4) and the import now works correctly. No clue why. I guess the .pyd file was written in that version of Python (I'm not the author of the file), but still I'm clueless as to what the exact origin of the problem is.

Solution 3:[3]

I've been through this error and what I found after a lot of investigation:-

issue was in Opencv==4.5.1 build from source with cuda and flag cuda_with_fast_math=on

I just rebuild OpenCV and disable

cuda_with_fast_math

and it works for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 minibands
Solution 3 Baher El Naggar