Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cimport numpy not working in cython; error message unclear (to me)

I'm new to cython (and python) and am trying to import numpy in a way that I can define variable types. My understanding is that I have to use "cimport numpy", which, however, fails, and I can't get anything out of the error message. Here's my code boiled down to the key part:

My actual module at this point includes nothing other than:

cimport numpy as np

This is my setup.py:

from distutils.core import setup
from Cython.Build import cythonize

setup(
name = "test",
ext_modules = cythonize('testcimport.pyx'),
)

This is what I get when I'm trying to compile:

In[1]: %run setup.py build_ext --inplace

Compiling testcimport.pyx because it changed. Cythonizing testcimport.pyx running build_ext building 'testcimport' extension gcc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -DNDEBUG -g -O3 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -I/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/include/python2.7 -c testcimport.c -o build/temp.macosx-10.6-x86_64-2.7/testcimport.o An exception has occurred, use %tb to see the full traceback.

SystemExit: error: command 'gcc' failed with exit status 1

In [2]: %tb

--------------------------------------------------------------------------- SystemExit Traceback (most recent call last) /Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where) 181 else: 182 filename = fname --> 183 builtin.execfile(filename, *where)

/Users/malte/Documents/setup.py in () 4 setup( 5 name = "test", ----> 6 ext_modules = cythonize('testcimport.pyx'), 7 )

/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/distutils/core.pyc in setup(**attrs) 167 raise 168 else: --> 169 raise SystemExit, "error: " + str(msg) 170 171 return dist

SystemExit: error: command 'gcc' failed with exit status 1

As you might have figured out from this output, I'm using Canopy on MacOSX.

Everything works fine if I use "import numpy" instead of "cimport numpy", but then I can't declare variables.

Thanks!

Malte

ps: I gather that I still need to do "import numpy" separately to get access to the normal python functions in numpy, but that doesn't change anything in this example - I still get the same error message.

like image 852
Malte Avatar asked Nov 16 '13 19:11

Malte


1 Answers

You have to use both:

cimport numpy
import numpy

The first gives you access to Numpy C API, so that you can declare the array buffers and variable types.

The second gives you access to Numpy Python functions.

Don't worry to use the same names ('numpy') in the same variable space because Cython handles this...


EDIT (from the comments): It seems that Cython is not finding the Numpy C API... could you try to add:

-I.../Python27/Lib/site-packages/numpy/core/include

to the compiler command line... (check if the path is correct for your system)

like image 135
Saullo G. P. Castro Avatar answered Sep 22 '22 23:09

Saullo G. P. Castro