cimport给出致命错误:找不到'numpy/arrayobject.h'文件

Dan*_*wer 4 python numpy cython

我正在尝试自学Cython但是在访问numpy时遇到了问题.当我使用'cimport'时,问题似乎就出现了.例如,导入以下函数时:

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
Run Code Online (Sandbox Code Playgroud)

我收到错误:

/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.
Run Code Online (Sandbox Code Playgroud)

任何关于如何解决这个问题的简单说明将非常感谢!

Dan*_*wer 11

好的,正如上面已经指出的那样,之前已经提出了类似的问题.例如:让distutils在正确的位置查找numpy头文件.我通过使用带有该行的设置文件使我的代码工作

include_dirs = [np.get_include()], 
Run Code Online (Sandbox Code Playgroud)

如:

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
   )
Run Code Online (Sandbox Code Playgroud)

我还没有尝试过使用pyximport,所以我不确定是否需要另外修复.