无法在 Python 中导入 dll 模块 [英] Can't import dll module in Python

查看:94
本文介绍了无法在 Python 中导入 dll 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来,我一直在努力在 Windows 上编译一个修改过的 libuvc 版本,现在我终于完成了,我似乎无法在 Python 上加载它.我已经在 Linux 机器上使用相同版本的 Python 编译并成功导入的这个库根本不喜欢 w10.

I've been stressin for a few days trying to compile a modified version of libuvc on windows and now that I've finally done it, I can't seem to load it on Python. This lib that I've already compiled and successfully imported using the same version of Python on Linux machines, doesn't like w10 at all.

系统

  • 赢得 10 64 位
  • python 3.8 64 位
  • libusb 1.022
  • 使用 MinGW64 编译的 libuvc.dll

问题

尝试时

import ctypes
import ctypes.util
name = ctypes.util.find_library('libuvc')
lib = ctypes.cdll.LoadLibrary(name)

我收到以下错误:

Could not find module 'C:\Program Files (x86)\libuvc\lib\libuvc.dll'.
Try using the full path with constructor syntax. 
Error: could not find libuvc!

问题是文件存在,因为它是由 util.find_library 找到的,但是 python 认为它不在它所在的位置,或者输出可能只是默认值.我在这里缺少什么?不仅无法加载模块,而且无法找到它,这可能是什么原因?对不起,我没有比这更多的输出了.

The issue is that the file exists since it was found by util.find_library, but python doesn't think it is where it is, or maybe the output is just the default. What am I missing here? What could be failing to be unable to not just load the module, but find it? I'm sorry I don't have more output than this.

P.S:我尝试以不同的方式重新格式化字符串,但消息没有改变.

P.S: I've tried reformatting the string in different ways, but the message doesn't change.

推荐答案

Python 3.8 开始,.dll 搜索机制改变了.

Starting with Python 3.8, the .dll search mechanism has changed.

根据[Python.Docs]: os.add_dll_directory(path)(重点是我的):

添加 DLL 搜索路径的路径.

Add a path to the DLL search path.

在解析导入的扩展模块的依赖项时使用此搜索路径(模块本身通过 sys.path 解析),也通过 ctypes.

This search path is used when resolving dependencies for imported extension modules (the module itself is resolved through sys.path), and also by ctypes.

所以,你可以这样做:

os.add_dll_directory("${path_to_working_dlls_directoy}")

您可以查看[SO]:PyWin32 和 Python 3.8.0(@CristiFati 的回答)(虽然看起来非常不同,但具有相同的原因),了解更多详情.

You can check [SO]: PyWin32 and Python 3.8.0 (@CristiFati's answer) (which although it seems very different, has the same cause), for more details.

这篇关于无法在 Python 中导入 dll 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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