Cython:"致命错误:numpy/arrayobject.h:没有这样的文件或目录"

Noo*_*bot 119 python numpy cython windows-7

我试图加快答案在这里使用用Cython.我尝试编译代码(在执行此处cygwinccompiler.py解释的hack 之后),但是出错了.任何人都可以告诉我,如果这是我的代码的问题,或Cython的一些深奥的微妙?fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated

以下是我的代码.提前致谢:

import numpy as np
import scipy as sp
cimport numpy as np
cimport cython

cdef inline np.ndarray[np.int, ndim=1] fbincount(np.ndarray[np.int_t, ndim=1] x):
    cdef int m = np.amax(x)+1
    cdef int n = x.size
    cdef unsigned int i
    cdef np.ndarray[np.int_t, ndim=1] c = np.zeros(m, dtype=np.int)

    for i in xrange(n):
        c[<unsigned int>x[i]] += 1

    return c

cdef packed struct Point:
    np.float64_t f0, f1

@cython.boundscheck(False)
def sparsemaker(np.ndarray[np.float_t, ndim=2] X not None,
                np.ndarray[np.float_t, ndim=2] Y not None,
                np.ndarray[np.float_t, ndim=2] Z not None):

    cdef np.ndarray[np.float64_t, ndim=1] counts, factor
    cdef np.ndarray[np.int_t, ndim=1] row, col, repeats
    cdef np.ndarray[Point] indices

    cdef int x_, y_

    _, row = np.unique(X, return_inverse=True); x_ = _.size
    _, col = np.unique(Y, return_inverse=True); y_ = _.size
    indices = np.rec.fromarrays([row,col])
    _, repeats = np.unique(indices, return_inverse=True)
    counts = 1. / fbincount(repeats)
    Z.flat *= counts.take(repeats)

    return sp.sparse.csr_matrix((Z.flat,(row,col)), shape=(x_, y_)).toarray()
Run Code Online (Sandbox Code Playgroud)

Rob*_*ern 166

在你的setup.py,Extension应该有参数include_dirs=[numpy.get_include()].

此外,您np.import_array()的代码中缺少.

-

示例setup.py:

from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=[
        Extension("my_module", ["my_module.c"],
                  include_dirs=[numpy.get_include()]),
    ],
)

# Or, if you use cythonize() to make the ext_modules list,
# include_dirs can be passed to setup()

setup(
    ext_modules=cythonize("my_module.pyx"),
    include_dirs=[numpy.get_include()]
)    
Run Code Online (Sandbox Code Playgroud)

  • 传递给`setup()`的`include_dirs`在最新的distutils中被忽略,它必须被传递给每个`Extension`,至少在mac上 (10认同)
  • 为什么我需要`np.import_array()`?那不是[Numpy C-API](http://docs.scipy.org/doc/numpy/reference/c-api.html)吗? (4认同)
  • `include_dirs=[numpy.get_include()]` 是一个很好的技巧,谢谢! (4认同)

Ste*_*nes 40

对于像您这样的单文件项目,另一种方法是使用pyximport.你不需要创建一个setup.py...如果你使用IPython你甚至不需要打开命令行......这一切都非常方便.在您的情况下,尝试在IPython或普通的Python脚本中运行这些命令:

import numpy
import pyximport
pyximport.install(setup_args={"script_args":["--compiler=mingw32"],
                              "include_dirs":numpy.get_include()},
                  reload_support=True)

import my_pyx_module

print my_pyx_module.some_function(...)
...
Run Code Online (Sandbox Code Playgroud)

您可能需要编辑编译器.这使得文件的导入和重新加载工作与.pyx文件相同.py.

资料来源:http://wiki.cython.org/InstallingOnWindows


Joh*_*die 11

该错误意味着在编译期间未找到numpy头文件.

尝试做export CFLAGS=-I/usr/lib/python2.7/site-packages/numpy/core/include/,然后编译.这是几个不同包的问题.ArchLinux中存在针对同一问题提交的错误:https://bugs.archlinux.org/task/22326

  • @NoobSaibot你得到了一个闻起来像窗户问题的lunix答案.... (3认同)