5

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!

Daniel Power
  • 645
  • 9
  • 22
  • 2
    http://stackoverflow.com/questions/2379898/make-distutils-look-for-numpy-header-files-in-the-correct-place -- should help you fix this. Basically you just need to help point the `C` compiler to the `numpy` header files. – qwwqwwq Dec 02 '13 at 20:52
  • Does this answer your question? [Make distutils look for numpy header files in the correct place](https://stackoverflow.com/questions/2379898/make-distutils-look-for-numpy-header-files-in-the-correct-place) – ead Apr 16 '20 at 19:18

1 Answers1

11

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.

Community
  • 1
  • 1
Daniel Power
  • 645
  • 9
  • 22