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

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

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

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.

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(rc: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 🙂 )

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

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 youre using PyTorch and loading your own DLLs youll have to rearrange your code to import all DLLs first. Confirmed w/ PyTorch 1.5.0 on Python 3.7

Related posts on python :

Leave a Reply

Your email address will not be published.