WindowsError:[错误126]找不到指定的模块 [英] WindowsError: [Error 126] The specified module could not be found

查看:157
本文介绍了WindowsError:[错误126]找不到指定的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在python中加载dll:

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:[错误126]找不到指定的模块

dll出现在指定的路径上,但是我不明白为什么会出现错误。

dll is present at the specified path, but I didn't understand why I'm getting the error.

推荐答案

像这样-通常是因为转换后的路径中存在反斜杠。

When I see things like this - it is usually because there are backslashes in the path which get converted.

例如-以下操作将失败-因为字符串中的\t转换为TAB字符。

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

有3种解决方案(如果是这样的话)

There are 3 solutions (if that is the problem)

a)使用双斜杠...

a) Use double slashes...

>>> import ctypes
>>> ctypes.windll.LoadLibrary("c:\\tools\\depends\\depends.dll")

b)使用正斜杠

>>> import ctypes
>>> ctypes.windll.LoadLibrary("c:/tools/depends/depends.dll")

c)使用RAW字符串(以r

c) use RAW strings (prefacing the string with r

>>> import ctypes
>>> ctypes.windll.LoadLibrary(r"c:\tools\depends\depends.dll")

尽管这第三个方法有效-我不时地感到它不被认为是正确的,因为RAW字符串是用于正则表达式的,多年来我一直在Windows中使用它作为Python的路径,而没有出现问题:))

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

这篇关于WindowsError:[错误126]找不到指定的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆