Pybind11:在Python解释器中尝试导入*.pyd时:“ImportError:未找到DLL”

2024-05-16 10:57:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在VisualStudio2019(社区)中构建了一个.pyd,它为一些只存在于LibRaw中的功能提供了包装。解决方案成功编译,没有任何警告或错误。该项目使用LibRaw、OpenCV和pybind11以及Python.h和相应的.lib-文件

当我尝试在Python解释器中导入.pyd时,我得到:

C:\Users\Tim.Hilt\source\repos\cr3_converter\Release>dir
 Datenträger in Laufwerk C: ist Acer
 Volumeseriennummer: EC36-E45E

 Verzeichnis von C:\Users\Tim.Hilt\source\repos\cr3_converter\Release

22.01.2020  11:28    <DIR>          .
22.01.2020  11:28    <DIR>          ..
22.01.2020  11:28               808 cr3_converter.exp
22.01.2020  11:28         3.068.361 cr3_converter.iobj
22.01.2020  11:28           785.552 cr3_converter.ipdb
22.01.2020  11:28             1.908 cr3_converter.lib
22.01.2020  11:28         4.190.208 cr3_converter.pdb
22.01.2020  11:28           953.856 cr3_converter.pyd
31.10.2019  16:22        26.408.085 IMG_0482_raw.CR3
               7 Datei(en),     35.408.778 Bytes
               2 Verzeichnis(se), 77.160.587.264 Bytes frei

C:\Users\Tim.Hilt\source\repos\cr3_converter\Release>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cr3_converter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing cr3_converter: The specified module was not found.
>>> import cr3_converter.pyd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing cr3_converter: The specified module was not found.
>>>

到所需的.dlls的路径(本例中为Python和OpenCV;LibRaw完全静态链接)在系统路径中设置

我跑了一遍,但没有发现任何可疑的东西Here is the corresponding Dependency Walker image。我还试用了另一个工具(Dependencies.exe,它本质上是对依赖项Walker的重写,但考虑了API-MS-WIN-CORE-....dlls),得到了一个错误,如下所示:

enter image description here

当我将鼠标悬停在缺少的.dll上时,我可以看到一个api-ms-win... module could not be found on disk。我已经搜索并找到了模块,并将其目录路径添加到系统路径中。现在模块不再以红色突出显示,但是C:\WINDOWS\SysWOW64\WS2_32.dll(屏幕截图顶部的红色突出显示)仍然显示缺少导入。这可能是个问题吗

我是如何制作.pyd的:

  • 创建了空的Visual Studio项目(Win32;Python也是32位安装)
  • 将项目设置更改为.dll-configuration和.pyd-fileextension
  • 添加了OpenCV、LibRaw和Pybind11头文件的include路径
  • 为OpenCV、LibRaw和Python3.8的链接器添加了路径和输入文件
  • 生成解决方案(无错误、无警告)
  • 试图在python解释器中导入结果.pyd

我看到过this question,OP遇到了库加载不同Python-.dlls的问题,但在我的例子中,Dependency Walker引用的库与path变量中的库相同


Tags: 项目路径sourcerelease错误opencvusersrepos
1条回答
网友
1楼 · 发布于 2024-05-16 10:57:24

我悄悄地假设Windows在与系统(/users)PATH-变量中列出的目录相同的目录中搜索.dll

然而,正如ProcMon所揭示的那样,情况并非如此。现在,我将丢失的.dll复制到包含.pyd的文件夹中,一切正常

相关问题 更多 >