[Numpy-discussion] How to call import_array() properly?

Bruce Sherwood Bruce_Sherwood at ncsu.edu
Sun Dec 26 17:26:36 EST 2010

In my Python code I have

import cvisual

and in my C++ code I have


import_array() in numpy/core/include/numpy/_multiarray_api.h is a macro:

#if PY_VERSION_HEX >= 0x03000000

#define import_array() {if (_import_array() < 0) {PyErr_Print();
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to
import"); return NUMPY_IMPORT_ARRAY_RETVAL; } }

Note that for Python 3 there is a change so that the macro returns
NULL, whereas for Python 2 it returned nothing.

On Windows and Mac, this works fine for Python 2, and it works fine
for a with Python 3, but with either Microsoft Visual Studio 2008 or
2010 this fails for Python 3 with the message "'void' function
returning a value", presumably due to the return NULL for Python 3,
something that doesn't bother the Mac.

So my dumb question is, how should I call import_array() from my
routine init_numpy() to get around this problem? I have found a
workaround, consisting of defining init_numpy to be of type int on
Windows with Python 3, but this seems like an odd kludge, since it
isn't needed on the Mac, and I think it's also not needed on Linux.

Bruce Sherwood

More information about the NumPy-Discussion mailing list