Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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, 2009 at 16:28

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.

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 5, 2014 at 14:16 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, 2016 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, 2018 at 21:35

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 :) )

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, 2019 at 20:43 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, 2019 at 12:36 It might be the library itself or its dependencies: see answer by @Mark Ucka below: stackoverflow.com/a/57793866/547270. – scrutari Jan 28, 2020 at 15:41

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.

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.

More detailed set of linker options for MinGW toolchain: -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic. – scrutari Jan 29, 2020 at 10:11

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.

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.

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.

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
                Hi @premvijay, thank you for your answer. Please consider using mardkdown editing in order to format and clarify your answer.
– Leonard
                Aug 12, 2020 at 5:26

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.

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

Trying to load .dll file in Python. Getting OSError: [WinError 126]. What am I doing wrong? See more linked questions