Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cimport gives fatal error: 'numpy/arrayobject.h' file not found

I'm trying to teach myself Cython but having problems accessing numpy. The problems seem to be when I use 'cimport'. For example when importing the following function:

cimport numpy
def cy_sum(x):
    cdef numpy.ndarray[int, ndim=1] arr = x 
    cdef int i, s = 0
    for i in range(arr.shape[0]):
            s += arr[i] 
    return s

I get error:

/Users/Daniel/.pyxbld/temp.macosx-10.6-x86_64-2.7/pyrex/test.c:314:10: fatal error: 'numpy/arrayobject.h' file not found

include "numpy/arrayobject.h"

1 error generated.

Any simple instructions on how to resolve this would be much appreciated!

like image 497
Daniel Power Avatar asked Dec 02 '13 16:12

Daniel Power


1 Answers

Ok, as has been pointed out above, similar questions have been asked before. Eg: Make distutils look for numpy header files in the correct place. I got my code to work by using a setup file with the line

include_dirs = [np.get_include()], 

As in:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np

ext  =  [Extension( "cy_test", sources=["cy_test.pyx"] )]

setup(
   name = "testing", 
   cmdclass={'build_ext' : build_ext}, 
   include_dirs = [np.get_include()],   
   ext_modules=ext
   )

I haven't yet tried using pyximport so I'm not sure whether another fix is required for that.

like image 72
Daniel Power Avatar answered Oct 01 '22 04:10

Daniel Power