编译多个模块时,import_array()出现Numpy / CAPI错误

我正在尝试编译一个C ++模块,用于scipy.weave ,它由几个头文件和源C ++文件组成。 这些文件包含广泛使用Numpy / C-API接口的类和方法。 但我没有弄清楚如何成功地包含import_array() 。 过去一周我一直在努力解决这个问题,我很疯狂。 我希望你可以帮助我,因为weave 帮助不是很清楚。

在实践中,我首先有一个名为pycapi_utils的模块,它包含一些用C对象与Python对象接口的例程。 它pycapi_utils.h文件pycapi_utils.h和源文件pycapi_utils.cpp例如:

 //pycapi_utils.h #if ! defined _PYCAPI_UTILS_H #define _PYCAPI_UTILS_H 1 #include  #include  #include  #include  #include  typedef std::tuple pykeyval; //Tuple type (string,Pyobj*) as dictionary entry (key,val) typedef std::list kvlist; //Declaration of methods PyObject* array_double_to_pyobj(double* v_c, long int NUMEL); //Convert from array to Python list (double) ... ... #endif 

 //pycapi_utils.cpp #include "pycapi_utils.h" PyObject* array_double_to_pyobj(double* v_c, long int NUMEL){ //Convert a double array to a Numpy array PyObject* out_array = PyArray_SimpleNew(1, &NUMEL, NPY_DOUBLE); double* v_b = (double*) ((PyArrayObject*) out_array)->data; for (int i=0;i<NUMEL;i++) v_b[i] = v_c[i]; free(v_c); return out_array; } 

然后我有一个进一步的模块model ,其中包含处理一些数学模型的类和例程。 它再次由头文件和源文件组成,如:

 //model.h #if ! defined _MODEL_H #define _MODEL_H 1 //model class class my_model{ int i,j; public: my_model(); ~my_model(); double* update(double*); } //Simulator PyObject* simulate(double* input); #endif 

 //model.cpp #include "pycapi_utils.h" #include "model.h" //Define class and methods model::model{ ... ... } ... ... double* model::update(double* input){ double* x = (double*)calloc(N,sizeof(double)); ... ... // Do something ... ... return x; } PyObject* simulate(double* input){ //Initialize Python interface Py_Initialize; import_array(); model random_network; double* output; output = random_network.update(input); return array_double_to_pyobj(output); // from pycapi_utils.h } 

上面的代码包含在Python的scipy.weave模块中

 def model_py(input): support_code=""" #include "model.h" """ code = """ return_val = simulate(input.data()); """ libs=['gsl','gslcblas','m'] vars = ['input'] out = weave.inline(code, vars, support_code=support_code, sources = source_files, libraries=libs type_converters=converters.blitz, compiler='gcc', extra_compile_args=['-std=c++11'], force=1) 

它无法编译给予:

 error: int _import_array() was not declared in this scope 

值得注意的是,如果我将源pycapi_utils.cpp ,一切正常。 但是我不想使用这个解决方案,因为在实践中我的模块需要包含在其他几个也使用PyObjects并需要调用import_array()

我正在寻找关于堆栈交换的这篇文章 ,但我无法弄清楚是否以及如何在我的情况下正确定义#define指令。 此post中的示例也不是我的情况,在main()的全局范围内调用import_array() ,而在我的情况下,在我的simulate例程中调用import_array() ,由main()构建由scipy.weave

我有一个类似的问题,因为你发布的链接指出,所有邪恶的根源是PyArray_API被定义为静态,这意味着每个翻译单元都有它自己的PyArray_API ,默认情况下用PyArray_API = NULL初始化。 因此,必须为每个*.cpp文件调用一次import_array() 。 在你的情况下,在pycapi_utils.cpp调用它并在pycapi_utils.cpp调用一次就model.cpp 。 您还可以在实际调用之前测试是否需要array_import:

 if(PyArray_API == NULL) { import_array(); } 

显然,如果我在pycapi_utils模块中包含一个简单的初始化例程,例如:

 //pycapi_utils.h ... ... void init_numpy(); //pycapi_utils.cpp ... ... void init_numpy(){ Py_Initialize; import_array(); } 

然后我在我的C代码中使用Numpy对象的任何函数/方法的开头调用这个例程,它可以工作。 也就是说,上面的代码被编辑为:

 //pycapi_utils.cpp ... ... PyObject* array_double_to_pyobj(...){ init_numpy(); ... ... } //model.cpp ... ... PyObject* simulate(...){ init_numpy(); ... ... } 

我唯一关心的是,是否有一种方法可以最小化对init_numpy()的调用次数,或者我是否必须从我在使用Numpy对象的CPP模块中定义的任何函数中调用它…