WindowsError: [Error 126] The specified module could not be found

Question:

I am loading a dll in python using following code:

if os.path.exists(dll_path):
     my_dll = ctypes.cdll.LoadLibrary(dll_path)

But I am continuously getting the following error

WindowsError: [Error 126] The specified module could not be found

dll is present at the specified path, but I didn’t understand why I’m getting the error.

Asked By: MA1

||

Answers:

When I see things like this – it is usually because there are backslashes in the path which get converted.

For example – the following will fail – because t in the string is converted to TAB character.

>>> import ctypes
>>> ctypes.windll.LoadLibrary("c:toolsdependsdepends.dll")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:toolspython271libctypes__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "c:toolspython271libctypes__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found

There are 3 solutions (if that is the problem)

a) Use double slashes…

>>> import ctypes
>>> ctypes.windll.LoadLibrary("c:\tools\depends\depends.dll")

b) use forward slashes

>>> import ctypes
>>> ctypes.windll.LoadLibrary("c:/tools/depends/depends.dll")

c) use RAW strings (prefacing the string with r

>>> import ctypes
>>> ctypes.windll.LoadLibrary(r"c:toolsdependsdepends.dll")

While this third one works – I have gotten the impression from time to time that it is not considered ‘correct’ because RAW strings were meant for regular expressions. I have been using it for paths on Windows in Python for years without problem 🙂 )

Answered By: markm

Also this could be that you have forgotten to set your working directory in eclipse to be the correct local for the application to run in.

Answered By: ceorron

Note that even if the DLL is in your path. If that DLL relies on other DLLs that are NOT in your path, you can get the same error. Windows could not find a dependency in this case. Windows is not real good at telling you what it could not find, only that it did not find something. It is up to you to figure that out. The Windows dll search path can be found here:
http://msdn.microsoft.com/en-us/library/7d83bc18.aspx

In my case, being sure all needed dlls were in the same directory and doing a os.chdir() to that directory solved the problem.

Answered By: Doo Dah

I met the same problem in Win10 32bit OS. I resolved the problem by changing the DLL from debug to release version.

I think it is because the debug version DLL depends on other DLL, and the release version did not.

Answered By: haiwuxing

In Windows, it’s possible. You will need to install: Visual C++ Redistributable for Visual Studio 2015. I had the same problem and I installed both version (Windows x86 and Windows x64). Apparently both are necessary to make it work.

Answered By: André Silva

Tried to specify dll path in different ways (proposed by @markm), but nothing has worked for me.
Fixed the problem by copying dll into script folder. It’s not a good solution, but ok for my purposes.

Answered By: Elephant

for me install Microsoft Visual C++ 2015 Redistributable Update 3 from https://www.microsoft.com/en-us/download/details.aspx?id=53587 solved it.

Answered By: Omer Anisfeld

If you are using GCC to compile it for Windows, it’s possible that the error is because dependent libraries can’t be found.

Using the -static flag if linking with GCC might fix that.

Answered By: Mark Ucka

On the off chance anyone else ever runs into this extremely specific issue..
Something inside PyTorch breaks DLL loading. Once you run import torch, any further DLL loads will fail. So if you’re using PyTorch and loading your own DLLs you’ll have to rearrange your code to import all DLLs first. Confirmed w/ PyTorch 1.5.0 on Python 3.7

Answered By: NestedCaveats

if you come across this error when you try running PyTorch related libraries you may have to consider installing PyTorch with CPU only version i.e. if you don’t have Nvidia GPU in your system.

Pytorch with CUDA worked in Nvidia installed systems but not in others.

Answered By: eswara amirthan s

There is a promising answer at Problem updating bokeh: [WinError 126] The specified module could not be found.

It hints at https://github.com/conda/conda/issues/9313.

There, you find:

It’s a library load issue. More details at
github.com/conda/conda/issues/8836 You probably have a broken conda
right now. You can use a standalone conda from
repo.anaconda.com/pkgs/misc/conda-execs to repair it:
standalone-conda.exe update -p C:ProgramDataAnaconda3
conda-package-handling You should get version 1.6.0, and the problems
should go away.

Thus, it might simply be a conda issue. Reinstalling standalone conda might repair the error. Please comment whoever can confirm this.

problem solved for me.
I changed version from pytorch=1.5.1 to pytorch=1.4 and typed the below command in anaconda prompt window

conda install pytorch==1.4.0 torchvision==0.5.0 -c pytorch
Answered By: premvijay

NestedCaveats solution worked for me.

Imported my .dll files before importing torch and gpytorch, and all went smoothly.

So I just want to add that its not just importing pytorch but I can confirm that torch and gpytorch have this issue as well. I’d assume it covers any other torch-related libraries.

Answered By: Reza Ahmed

This is probably because a runtime dependency of one of the DLLs was not found on your system. I think that the expected Microsoft Visual C runtime DLL is missing from your system.

Install this:

https://www.microsoft.com/en-US/download/details.aspx?id=40784

Answered By: D_Dog

I fixed this issue by installing VC redistributable for Visual Studio 2012 and the up-to-date Visual Studio versions. After reboot, the problem has gone.

Answered By: Taehyun Yoon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.