使用python从其他目录加载.dll时出错ctypes.CDLL()

2024-06-08 15:13:56 发布

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

我必须遵循以下目录结构:

MainProject  
    |    ...project files  
    |    rtlsdr\  
    |    |    rtlsdr.dll
    |    |    ...other .dll's etc.  

我使用库ctypes中的函数CDLL()来加载rtlsdr.dll。当我的工作目录是rtlsdr\时,它可以正常工作:

^{pr2}$

但是,当我试图从另一个目录加载文件时:

$ cd MainProject
$ python
> from ctypes import *
> d = CDLL('rtlsdr\\rtlsdr.dll')

我得到一个错误:

WindowsError: [Error 126] The specified module could not be found.

这里有什么问题?在


Tags: 文件函数目录projectetccdfilesctypes
1条回答
网友
1楼 · 发布于 2024-06-08 15:13:56

DLL可能有其他不在工作目录或系统路径中的DLL依赖项。因此,如果没有明确指定,系统就无法找到这些依赖项。我发现的最佳方法是将包含依赖项的目录的位置添加到系统路径:

import os
from ctypes import *
abs_path_to_rtlsdr = 'C:\\something\\...\\rtlsdr'
os.environ['PATH'] = abs_path_to_rtlsdr + os.pathsep + os.environ['PATH']
d = CDLL('rtlsdr.dll')

关闭当前会话后,PATH变量将返回其原始状态。在

另一个选择是更改工作目录,但这可能会影响其他模块导入:

^{pr2}$

相关问题 更多 >