How does import_array in numpy C API work?
NickName:W. Verbeke Ask DateTime:2018-10-16T14:05:57

How does import_array in numpy C API work?

I am trying to convert a c-style array in c++ to a numpy array and ran into problems when trying to use the "PyArray_SimpleNewFromData" function. It turns out I need to call

import_array()

Though I do not understand how to call this function. Whenever I try calling it I get compiler error which I do not manage to understand. For instance writing the following simple script:

#include <Python.h>
#include <numpy/arrayobject.h>

int main(){
    Py_Initialize();
    import_array();
    Py_Finalize();
    return 0;
}

produces the compiler error

error: return-statement with no value, in function returning 'int' [-fpermissive] import_array();

I looked at several examples, such as :

Numpy C-Api example gives a SegFault

PyArray_SimpleNewFromData example

https://codereview.stackexchange.com/questions/92266/sending-a-c-array-to-python-numpy-and-back

Numpy/CAPI error with import_array() when compiling multiple modules

But whatever I try (even when seemingly following those examples) I seem to run into the compiler error above. What am I missing or doing wrong? An explanation on how the import_array() function should be called would be very welcome. Thanks for the help!

Update:

I am using python 2.7.11, and I think it might be related to what is discussed here :

https://github.com/clemenscorny/brisk/issues/3

but I still have no idea how to fix it.

Copyright Notice:Content Author:「W. Verbeke」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/52828873/how-does-import-array-in-numpy-c-api-work

Answers
Bérenger 2019-04-19T10:49:54

Reading the source code of import_array() from numpy, it is a macro that mainly calls the _import_array() regular function, and then does some weird exception handling. If you call _import_array() instead of import_array(), the compilation error disappears. And, at least in my case, the behavior is then correct (i.e. no segfault when calling Numpy C API functions)",


More about “How does import_array in numpy C API work?” related questions

How does import_array in numpy C API work?

I am trying to convert a c-style array in c++ to a numpy array and ran into problems when trying to use the "PyArray_SimpleNewFromData" function. It turns out I need to call import_array() Thoug...

Show Detail

Python C-API and Numpy: core dump on import_array

//testNumpy.c #include&lt;Python.h&gt; #include&lt;numpy/arrayobject.h&gt; #include&lt;stdio.h&gt; int main(){ printf("import_array\n");fflush(stdout); import_array(); printf("

Show Detail

import_array() error while embedding python and numpy to C++

I write a simple code trying to use numpy in C++. My OS is ubuntu16.04, with gcc5.4.0, Python2.7.12 and numpy1.15.0. Here is my codetest2.cpp: #include "Python.h" #include "numpy/arrayobject.h"...

Show Detail

import_array doesn't work in debug build in embedded Python C API

I have the following problem: I am using the embedded Python C API from C++ to execute Python code. Everything works so far in Release Mode, but as soon as I start to run the Debug Mode, I get the ...

Show Detail

Segfault when import_array not in same translation unit

I'm having problems getting the NumPy C API to properly initialize. I think I've isolated the problem to calling import_array from a different translation unit, but I don't know why this should mat...

Show Detail

Numpy/CAPI error with import_array() when compiling multiple modules

I am trying to compile a C++ module to use in scipy.weave that is composed of several headers and source C++ files. These files contain classes and methods that extensively use the Numpy/C-API inte...

Show Detail

Python import_array makes it impossible to kill embedded python with ctrl-c

I'm trying to use Numpy in an embedded Python. I use Python 3.4 and boost::python with Boost 1.57. To prevent Python from setting a signal handler that would prevent me from kill my program with Ct...

Show Detail

Trouble with Numpy C API

Code (based on Numpy C-Api example gives a SegFault. The link to the tutorial they were following is dead, and I haven't found a good tutorial): #include &lt;Python.h&gt; #define NPY_NO_DEPRECATE...

Show Detail

Segmentation Fault calling Numpys import_array on Mac M1

for an application I am trying to use NumPy from C++ on a Mac M1. Unfortunately I get a segmentation fault when calling import_array(). My small test program looks like this: #define PY_SSIZE_T_CLE...

Show Detail

import_array segfaults when called from a dynamic library

The goal is to create a numpy array in c++ and access it in python. The block of code below runs fine when run as a program. However, if I use ctypes and run the function, it segfaults on _import_...

Show Detail