python - cimport 给出 fatal error : 'numpy/arrayobject.h' file not found

标签 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

我得到错误:

/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.

任何有关如何解决此问题的简单说明将不胜感激!

最佳答案

好的,上面已经指出了,之前也有人问过类似的问题。 例如:Make distutils look for numpy header files in the correct place . 我通过使用带有以下行的设置文件使我的代码正常工作

include_dirs = [np.get_include()], 

如:

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
   )

我还没有尝试过使用 pyximport,所以我不确定是否需要另一个修复程序。

关于python - cimport 给出 fatal error : 'numpy/arrayobject.h' file not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20333128/

相关文章:

python - object.filter() 和外键 NULL

Python Selenium Web 从图表中抓取数据

python - 字节码编译给 Python 代码带来了多少加速?

python - 实现两个二维数组之间的加权平均值

Python:通用导入

python - 沿 Axis = 0 查找 3D np.array 的最大值

python - 很难启动 Rabbitmq Server 并想知道为什么不断收到此错误 init :do_boot/3 line 817

python - 如何使用 Cython 将 Python 列表传递给 C 函数

python - cython 问题 : 'bool' is not a type identifier

cython - Cython中的extra_compile_args