且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Cython:从C程序调用Python代码

更新时间:2023-11-10 18:28:52

在此代码中, initlibcimp()实际上失败了,但是您不会立即看到它,因为该错误是通过设置python异常报告的。我不是100%肯定这是正确的方法,但是我可以通过在该调用下面添加以下代码来看到错误:

In this code, the initlibcimp() actually fails, but you don't see it right away because the error is reported by setting a python exception. I'm not 100% sure this is the correct way to do this, but I could see the error by adding the following code below that call:

if (PyErr_Occurred())
{
    PyErr_Print();
    exit(-1);
}

然后,程序将输出:

Traceback (most recent call last):
  File "libcimpy.pyx", line 1, in init libcimpy (libcimpy.c:814)
    cimport cimpPy
ImportError: No module named cimpPy

cimpPy 模块尚未定义,是您需要在调用 initlibcimp之前调用 initcimpPy()

The reason that the cimpPy module is not yet defined, is that you need to do a call to initcimpPy() before calling initlibcimp.