简体   繁体   中英

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.

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

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

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.

这也可能是您忘记将 eclipse 中的工作目录设置为应用程序运行的正确本地目录。

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.

对我来说,从https://www.microsoft.com/en-us/download/details.aspx?id=53587安装 Microsoft Visual C++ 2015 Redistributable Update 3 解决了它。

if you come across this error when you try running PyTorch related libraries you may have to consider installing PyTorch with CPU only version ie 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

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

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.

检查你的dll名称..它不能包含任何大写字母..它必须包含所有小写字符才能成功编译

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM