在Python中导入C++ DLL失败
根据《扩展和嵌入Python解释器》这份文档,我创建了一个VC项目,并成功生成了一个名为“spam_d.dll”的dll文件。
主要代码是:
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;
if (!PyArg_ParseTuple(args, "s", &command))
return NULL;
sts = system(command);
return Py_BuildValue("i", sts);
}
static PyMethodDef SpamMethods[] = {
{"system", spam_system, METH_VARARGS, "Execute a shell command."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initspam(void)
{
(void) Py_InitModule("spam", SpamMethods);
}
然后我在Python中输入了以下命令:
import spam [39003 refs] spam.system("pwd") /SVN/Python/PCbuild 0 [39005 refs]
看起来运行得很正常。 但是当我把dll的名字从spam_d.pyd改成spam.pyd时,Python就找不到这个模块了。
>>> import spam
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named spam
[39005 refs]
从第一个情况来看,Python似乎能够正确建立“import spam”和“spam_d.pyd”之间的关系。
那么,Python是怎么知道“spam”模块是“spam_d.pyd”,而不是“spam.pyd”的呢?
有没有相关的文档提到过这个问题?
1 个回答
1
这个意思是,Python在尝试连接一个后缀为_d.pyd的调试库,因为它是一个调试版本。如果你想连接spam.pyd这个文件,你需要使用发布版本。