如何使用Python查找dll

问题描述 投票:4回答:1

我有一些应该加载.dll的代码。我正在将Python版本3.0.17114.1与Visual Studio 2017预览版一起使用。这是一个代码片段:

from ctypes.util import find_library
theDll = find_library('DsiLibrary_dll')

theDll出来为“ None”,这意味着找不到它。

请注意,谷歌搜索表明我不应该添加.dll:

#from "https://docs.python.org/3/library/ctypes.html"
#name is the library name without any prefix like lib, suffix like .so, .dylib

我检查了SO,在一个示例中,一个人确实使用了.dll。所以我尝试添加.dll但没有帮助。然后,我也尝试使用cdll.find_library,但这也不起作用。

所以我添加了这个以确保.dll存在:

print ("Current directory:")
os.system("cd")
os.system("dir /s/b DsiLibrary_dll.dll")

输出是这个:

Current directory:
L:\DSI (His) Try 5\Windows Apps\PythonSample
L:\DSI (His) Try 5\Windows Apps\PythonSample\DsiLibrary_dll.dll

显示DLL在那里。

我是Python的新手,但希望有人可以在这里提供帮助。

python dll
1个回答
0
投票

将dll添加到PATH 的一种方法是将其馈送到os.add_dll_directoryPython 3.8 addition:未从PATH搜索DLL)是使用pathtub.ensure_dll。 (我是包裹的作者)

要求

Windows。如果需要使用永久性的PATH编辑,则需要Powershell(在pathtub v.1.1.2中)]

安装

pip install pathtub

确保找到DLL

from pathtub import ensure_dll
dll_folder = r'C:\my favourite\dlls'
ensure_dll(dll_folder)
  • ensure_dll应该在每次脚本启动时被调用,例如,在程序包的C0中(或类似的输入脚本)。

永久添加到PATH

如果只想永久添加到PATH,则可以使用__init__.py(从add_to_path,例如参见pathtub),但是我不建议在每种情况下都添加依赖PATH的DLL

另请参阅this上的路径库文档。

© www.soinside.com 2019 - 2024. All rights reserved.