繁体   English   中英

为什么 DLL 没有像其他库一样在 python 脚本中导入?

[英]Why DLL is not imported in the python script as the other libraries?

我知道 DLL 是一个库,它有方法、类……据我所知使用它的内容,它被加载到 python 但没有作为其他库导入,例如 numpy。 这是否意味着它无法导入并且必须加载? 如果是这样,为什么?

DLL 本质上不同于 python 模块(例如 numpy)。 一个 python 模块只是一个包含 python 代码的文件,如果相应的模块被导入就可以使用。 如果你输入

import numpy

然后执行 python 安装目录中某处的 python 文件,并且可以在调用 python 程序中使用定义的函数、类和对象。

DLL 是一个包含编译函数和对象的共享库。 DLL 通常是通过将 C 或 C++ 代码编译到共享库中来创建的,但也可以从其他编程语言构建。 python 解释器不能仅仅读取和执行它们,因为那里没有 pyhton 代码。 这就是您不能使用import语句导入 DLL 的原因。

然而,这并不意味着在 python 中导入 DLL 函数是不可能的。 python 标准库提供了ctypes模块,它允许您在运行时加载 DLL 并调用其中的函数。 However, because the DLL does know nothing about python objects in general, there are a lot of type conversions in the ctypes module, that you must use to convert the python variables to C data types if you want to pass them to functions in the DLL . 有关这方面的更多信息,请参阅 python 标准库文档

请注意,例如 python 模块numpy也可能在引擎盖下加载 DLL。 所以只是因为你导入了一个 python 模块,这并不意味着没有加载任何 DLL。 In fact it is common for libraries written in C or C++ to be compiled into a DLL and then also provide a thin layer of python code (that can be imported as python module) to load that DLL, do the type conversion and call the functions在里面。 您在问题中提到的方法和类很可能来自这样的接口模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM