18

Recently I start to get ImportError: DLL load failed: error when I import different libraries (for example scikit-learn or scipy and some others).

My assumptions is that I have broken something when I was trying to pip install opencv.

So, my question is how to resolve this problem that seems to be not library specific?

Can I pip install DLL or something like that? Can I just reinstall the whole Python? I am working on Windows. My version of Python is Python 2.7.10 :: Anaconda 2.3.0 (64-bit).

ADDED

If I print sys.path I get this:

['',
 'C:\\Anaconda\\Scripts',
 'C:\\Anaconda\\python27.zip',
 'C:\\Anaconda\\DLLs',
 'C:\\Anaconda\\lib',
 'C:\\Anaconda\\lib\\plat-win',
 'C:\\Anaconda\\lib\\lib-tk',
 'C:\\Anaconda',
 'C:\\Anaconda\\lib\\site-packages',
 'C:\\Anaconda\\lib\\site-packages\\Sphinx-1.3.1-py2.7.egg',
 'C:\\Anaconda\\lib\\site-packages\\cryptography-0.9.1-py2.7-win-amd64.egg',
 'C:\\Panda3D-1.9.2-x64',
 'C:\\Panda3D-1.9.2-x64\\bin',
 'C:\\Anaconda\\lib\\site-packages\\win32',
 'C:\\Anaconda\\lib\\site-packages\\win32\\lib',
 'C:\\Anaconda\\lib\\site-packages\\Pythonwin',
 'C:\\Anaconda\\lib\\site-packages\\setuptools-17.1.1-py2.7.egg',
 'C:\\Anaconda\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\myname\\.ipython']

What worries me is that there is a mixture of 32 and 64 versions. Another thing, maybe I just have different Pythons and I just need to call the proper one?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • I'm not sure how to get you out of this problem, but if you start using virtualenv you'll limit the scope of such problems in the future.. – thebjorn Dec 28 '16 at 16:00
  • What's the whole error message? – gzc Dec 28 '16 at 16:02
  • The whole error message is in German. It tells something like: `ImportError: DLL load failed: the given module is not found.` – Roman Dec 28 '16 at 16:04

8 Answers8

11

If anyone comes across this issue in Python > 3.8 with Windows, dll's are only loaded from trusted locations https://docs.python.org/3/whatsnew/3.8.html#ctypes This can be fixed by adding the dll path using os.add_dll_directory("PATH_TO_DLL")

sam
  • 1,005
  • 1
  • 11
  • 24
  • 5
    Thanks @sam. This should be the correct answer! Some more detail for python beginners using Anaconda/Spyder on how to get this DLL path. 1) In console Anaconda CMD type `echo %path%` to see where your anaconda is installed. 2) In spyder's python console you can enter command `import os`, then run the `os.add_dll_directory` as sam suggests. Mine was: `os.add_dll_directory("C://Users/MYNAME/Anaconda3/DLLs")`. Windows users do not forget to change your slash direction from \ to / and first slash after : needs 2 //. – micstr Oct 13 '21 at 08:59
  • 1
    For clarity - this will only work with Python 3.8 and newer. OP says it, but I read it differently than what was intended. I'm on Python 3.7. Python3.7: `module 'os' has no attribute 'add_dll_directory' ` – PfunnyGuy Jul 19 '22 at 18:54
  • 1
    [\[SO\]: Can't import dll module in Python (@CristiFati's answer)](https://stackoverflow.com/a/59333711/4788546). – CristiFati Jan 07 '23 at 13:07
4

I have managed to resolve the problem by reinstalling Python. First, I have uninstalled Python (like any other program in Windows). Then I have installed Anaconda distribution of Python. The problem is not present anymore.

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    I was getting the same error ( ImportError: DLL load failed ) on trying to pip or use jupyter notebook. Realized I had two versions of Python (including the one that comes with Anaconda). Uninstalling the other version of Python has solved the problem. Thanks! – Indi Dec 29 '16 at 09:38
  • 1
    I really didn't want to do this but then after digging I found that I must have accidentally installed a 64-bit 2.7 update over a previous 32-bit 2.7 installation so I got this ImportError but only on rarely-used modules. Alas, reinstalling of course fixed it. – dash-tom-bang Apr 02 '19 at 15:30
3

I just encountered this exact problem and was struggling to solve it. I tried to reinstall anaconda, I reinstalled the relevant packages, I changed my environment - BUT NOTHING WORKED! eventually, after few hours, I managed to solve this problem with some few easy steps as described here :)

just type in the command line (one code-line at a time): conda install numpy then, conda install scipy then, pip install -U seaborn then, pip install -U numpy and finally, pip install -U scipy

That's it :) - it is now working! (it also fixed the error for all of the other libraries as well, such as sklearn, matplotlib etc.)

Ofir Shorer
  • 446
  • 1
  • 6
  • 16
1

This is an old question, and sadly pops up in search results. The solution that actually works:

If re-installing python didn't work, you simply need to update pywin32

pip install pywin32==300

Read more here: ImportError: DLL load failed while importing shell

mohghaderi
  • 2,520
  • 1
  • 19
  • 12
0

So I faced a similar issue; uninstalling and reinstalling Anaconda was the only way I found to fix it. To remove Anaconda and all its residual files I used the iobit uninstaller software, you can download it here:

https://www.iobit.com/en/recommend/iu.php

Divinity
  • 93
  • 2
  • 7
0

In my case, the VSCode discovery kept failing due to a invalid DLL import during test discovery. In the actual running case, the enviroment is established and the DLL will work. To get around it, I wrapped the import for the pytest in a check to see if pytest was used in discovery or testing

(Note: the DLL failure won't be trapped by the try: except: block during discovey)

Module:

...

try:
    # avoid incorrect import failures during discovery and unit test
    if not is_running_in_test():
        import MyNiceDLLmodule

except:
    # real failures during runtime
    raise Exception("Failed to import module")
...

To handle cases of module import during pytest disovery and unit test:

def is_running_in_test() -> bool:
    import os
    import sys

    # handles most unit test discovery and test situations for VSCode

    is_pytest = os.path.basename(sys.argv[0]) in ["pytest", "py.test"]
    is_pytest |= "vscode_pytest" in sys.argv[0]
    is_test_discovery = "discover" in sys.argv and "pytest" in sys.argv
    is_test_discovery |= r"pytest\__main__.py" in sys.argv[0]

    return is_pytest or is_test_discovery
RexBarker
  • 1,456
  • 16
  • 14
-1

I don't know but I opened the adminstrator cmd if don't know just hover over command promp and right click and you'll see the option of open in admistrator mode click over it just uninstall using pip uninstall package_name and don't close because the package_will be cached down and when you again command pip install package_name it should work ,just because it worked for me LOL if you close it you have to download it again like ffpyplayer is of 25 mbs so why to do that

-2

You can try to activate an enviroment. Activating environments is essential to making the software in the environments work well. Activation entails two primary functions: adding entries to PATH for the environment and running any activation scripts that the environment may contain. (Only work on conda 4.6 and later versions)

To activate, execute this first on cmd:

conda activate

The, run the jupyter notebook on cmd too

jupyter notebook

This work for me when I was trying to use "seaborn" and anothers packages like numpy, sklearn, and so on.