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!

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

it seems in the latest distutils include_dirs gets ignored (at least on OSX)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.