繁体   English   中英

Python读取dll抛出OSError:[WinError 126]

[英]Python reading dll throws OSError: [WinError 126]

我显然不是第一个在使用 python 读取 .dll 文件时遇到问题的人。 这样的例子可以在那里找到WindowsError: [Error 126] when loading a OS with ctypes , WindowsError: [Error 126] The specified module could not be found or https://github.com/apache/incubator-mxnet/issues/6313 . 知道问题的范围后,我检查了我的 dll 的路径是否正确。 我什至做了一个小的 python 脚本来最低限度地测试它,添加尽可能多的我能想到的路径:

import sys
import os
from ctypes import *

if __name__ == '__main__':
    print(sys.path)
    sys.path.append(r"C:\Program Files (x86)\OpenBLAS\bin")
    pathWin = os.environ["PATH"]
    pathWin = pathWin.split(";")
    sys.path = sys.path + pathWin
    print(sys.path)
    dllToLoad = "F:/installMxnet/mxnet/build/Debug/libmxnet.dll"
    cdll.libmxnet = cdll.LoadLibrary(dllToLoad)

我仍然以这个错误结束:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.1\helpers\pydev\pydevd.py", line 1664, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.1\helpers\pydev\pydevd.py", line 1658, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.1\helpers\pydev\pydevd.py", line 1068, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "main.py", line 16, in <module>
    cdll.libmxnet = cdll.LoadLibrary(dllToLoad)
  File "C:\Users\educrocq\AppData\Local\Programs\Python\Python36\lib\ctypes\__init__.py", line 426, in LoadLibrary
    return self._dlltype(name)
  File "C:\Users\educrocq\AppData\Local\Programs\Python\Python36\lib\ctypes\__init__.py", line 348, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

根据我的阅读,这可能是因为 dll 有一个无法找到的依赖项。 (而且我猜想找不到它的依赖的依赖会引发同样的问题,等等......)。 似乎问题来自 Windows,它的输出消息并不详细。

但是我需要知道在我的情况下找不到哪个 dll,因为这个 dll 依赖于很多……有没有办法让哪个 dll 丢失?

好的,多亏了@eryksun 的反复帮助和来自此处解释的 C++ 等效问题的信息, DLL 加载库 - 错误代码 126 ,我设法找到了哪个 dll 丢失但未加载。 sysinternal 工具非常有助于动态了解缺少哪个。 我当时犯的一个错误是期望同一文件夹中的每个 dll 都会被加载。 实际上,只有在 Windows 路径中才会加载 dll。 因此,创建一个包含所有未解决我的问题的 dll 的文件夹。

但是,我深感遗憾的是,错误消息中缺少提供的信息来说明缺少哪个 dll ......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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