python调dll时报FileNotFoundError: Could not find module ‘xxx.dll‘(or one of its dependencies)的解决方案
如题所述, 在windows下,python调用dll文件时,无论是使用ctypes.cdll.LoadLibrary(`xxx.dll`)
,还是使用ctypes.CDLL('xxx.dll')
,均报如下错误:
FileNotFoundError: Could not find module ‘xxx.dll’ (or one of its dependencies). Try using the full path with constructor syntax.
有文章说是因为加载的dll缺少依赖库,使用VS的dumpbin.exe查看以后,发现依赖的dll都存在,所以博主遇到的应该不是此类问题。
dumpbin的使用方式如下:
dumpbin.exe /dependents xxx.dll
还有文章说是dll路径不正确,先使用os.add_dll_directory
将路径加载进来,结果发现也不生效。。。
后来发现,使用ctypes.CDLL('xxx.dll',winmode=0)
可以完美解决该问题。
原因是因为python3.8及以上版本的dll搜索机制变更导致的,目前暂时的解决方法:使用CDLL
方法加载dll并设置参数winmode=0
参考链接:
作者:罗伯特祥