在Python中导入C++ DLL失败

2024-10-02 18:18:57 发布

您现在位置:Python中文网/ 问答频道 /正文

根据“扩展和嵌入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重命名为垃圾邮件.pyd. Python找不到模块。在

^{pr2}$

从第一种情况来看,python可以正确地设置“import spam”和“spam_d.pyd”之间的关系。在

python怎么知道“spam”模块是“spam_d.pyd”,但不是垃圾邮件.pyd"? 在

有没有文件提到这一点。在


Tags: 文件pyreturnargsstaticspamsystemnull