50

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.

Dan
  • 9,391
  • 5
  • 41
  • 73
MA1
  • 2,767
  • 5
  • 35
  • 51
  • Can you elaborate on the DLL you are trying to import and what that DLL does? It may itself be trying to load another DLL that it can't find. – santosc Dec 21 '09 at 16:28
  • The answer to [this question](http://stackoverflow.com/questions/2334627/error-loading-dll-in-path-with-parenthesis-using-ctypes-python) solved it for me - duplicate? – Junuxx Oct 01 '12 at 16:44

15 Answers15

38

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.

Doo Dah
  • 3,979
  • 13
  • 55
  • 74
  • 4
    Just to mention that "os.chdir()" (while I'm not sure if this is the correct way of handling the problem) helped me after trying options mentioned in the marked answer – JavierQQ23 Nov 05 '14 at 14:16
  • 1
    An alternative to using os.chdir() is to be sure the directory with the dlls is in your path. Either way enables Windows to find the dlls – Doo Dah May 20 '16 at 15:24
  • I have a very simple dll with no other dependancies except MSVC C++. It seems to be not finding a link to that. How do i solve it – raaj Jun 24 '18 at 21:35
20

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:\tools\depends\depends.dll")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\tools\python271\lib\ctypes\__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "c:\tools\python271\lib\ctypes\__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:\tools\depends\depends.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 :) )

markm
  • 896
  • 9
  • 13
  • 1
    I know this is an old thread, but I found in Python 3.6 that this works. {print("Adding dll path ",combinedPath) os.environ['PATH'] = ';'.join([os.environ['PATH'], DLLPath]) dll = windll.LoadLibrary('D:/projects/trunk/distributed_processing/my_custom_code.dll')} how the heck do you get line returns in answers????? – Richard Keene Jun 19 '19 at 20:43
  • 3
    If the path is invalid - the predicate "os.path.exists(dll_path)" will return False! This solution is not appropriate with the question. – Azriel Berger Nov 27 '19 at 12:36
  • It might be the library itself or its dependencies: see answer by @Mark Ucka below: https://stackoverflow.com/a/57793866/547270. – scrutari Jan 28 '20 at 15:41
7

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
  • 71
  • 1
  • 2
3

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.

haiwuxing
  • 1,162
  • 10
  • 12
3

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.

Mark Ucka
  • 56
  • 4
  • More detailed set of linker options for MinGW toolchain: `-static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic`. – scrutari Jan 29 '20 at 10:11
0

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.

ceorron
  • 1,230
  • 1
  • 17
  • 28
0

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.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
0

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.

Elephant
  • 675
  • 1
  • 8
  • 18
0

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

Omer Anisfeld
  • 1,236
  • 12
  • 28
0

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.

0

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.

questionto42
  • 7,175
  • 4
  • 57
  • 90
0

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
lux7
  • 1,600
  • 2
  • 18
  • 34
  • Hi @premvijay, thank you for your answer. Please consider using [mardkdown editing](https://stackoverflow.com/editing-help) in order to format and clarify your answer. – Leonard Aug 12 '20 at 05:26
0

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.

0

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

D_Dog
  • 165
  • 9
0

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.