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

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.

This question is related to python ctypes

The answer is


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


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.


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.


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.


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.


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:\ProgramData\Anaconda3 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.


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.


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.


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

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.


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


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.