Python dll 扩展导入

标签 python dll

我为我的 Python 创建了扩展并创建了一个 abcPython.dll。如何将此 dll 导入我的 Python 脚本?

当我尝试使用以下命令导入它时收到错误消息

>>>import abcPython
>>>Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     ImportError: No module named abcPython
>>>

我手动创建了一个名为PYTHONPATH的系统环境变量,其中存储了abcPython.dll的路径,但仍然存在错误。

我该如何解决这个问题?

最佳答案

关注Building C and C++ Extensions on Windows仔细 - 在第 7 小节中,它说:

The output file should be called spam.pyd (in Release mode) or spam_d.pyd (in Debug mode). The extension .pyd was chosen to avoid confusion with a system library spam.dll to which your module could be a Python interface
...
Changed in version 2.5: Previously, file names like spam.dll (in release mode) or spam_d.dll (in debug mode) were also recognized.

尝试重命名您的 DLL 以使用 .pyd 扩展名而不是 .dll

(谢谢,Peter Hansen)

引用指向一个C example ,它明确包含一个 INIT 函数, PyMODINIT_FUNC initexample(void)。生成的 DLL 应重命名为 example.pyd :

#include "Python.h"

static PyObject *
ex_foo(PyObject *self, PyObject *args)
{
    printf("Hello, world\n");
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef example_methods[] = {
    {"foo", ex_foo, METH_VARARGS, "foo() doc string"},
    {NULL, NULL}
};

PyMODINIT_FUNC
initexample(void)
{
    Py_InitModule("example", example_methods);
}

关于Python dll 扩展导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1979793/

相关文章:

c++ - 导出 DLL - 删除头文件

c++ - 为什么我的 VBA 在 C++ DLL 中找不到函数?

c++ - 如何获取应用程序的最小化/最大化事件

c - Windows 上混合 Julia 和 C 项目的最小工作示例

Python 尝试迭代多维列表的一个元素

python - 基于值是否在其他列表中的 Numpy 掩码

python - 将字符串与列表中的单词匹配

尝试通过 pip 安装 mysql-connector 的 Python 错误

深度学习环境下Python包安装错误

c++ - 如何打开用 C++ 创建的 DLL 文件以查看类、方法和源代码?