[Numpy-discussion] problem importing NumPy from C/C++

Jim Bosch talljimbo@gmail....
Fri Apr 13 15:21:54 CDT 2012


One of my colleagues is having some difficulty with some of my code that 
uses the NumPy C-API; I think we're seeing the C import_array() function 
claim that it can't find NumPy, even though "import numpy" works just 
fine in Python.

Here's a minimal embedded-Python program that is enough to cause problems:

---------
#include "Python.h"
#include "numpy/arrayobject.h"
void doImport() {
   import_array();
}
int main()
{
   int result = 0;
   Py_Initialize();
   doImport();
   if (PyErr_Occurred()) {
     result = 1;
   } else {
     npy_intp dims = 2;
     PyObject * a = PyArray_SimpleNew(1, &dims, NPY_INT);
     if (!a) result = 1;
     Py_DECREF(a);
   }
   Py_Finalize();
   return result;
}
---------

That compiles and links fine, but when running it we see:
---------
ImportError: numpy.core.multiarray failed to import
---------
and the exit code is 1.

Note that we can run python and successfully import numpy there, in 
what's otherwise the same environment, so it doesn't appear to be 
PYTHONPATH issue (or, I think, an environment issue, though there might 
be a difference between embedding and extending).

All of the above does work on a number of other systems, so I suspect 
something went wrong with this particular NumPy install, but it claims 
to have been successful.  This is with gcc 4.1.2, RHEL 5.  Any ideas?

Thanks!

Jim Bosch


More information about the NumPy-Discussion mailing list