且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在 Python 中导入 dll 模块

更新时间:2022-06-09 05:22:37

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.