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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With