我为我的 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/