168

我正在尝试使用 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()
4

8 回答 8

227

在你的setup.pyExtension应该有说法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()]
)    
于 2013-02-02T01:26:29.747 回答
50

对于像您这样的单文件项目,另一种选择是使用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(...)
...

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

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

于 2013-02-03T23:24:34.870 回答
16

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

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

于 2013-02-02T00:46:54.770 回答
2

如果您懒得编写设置文件并找出包含目录的路径,请尝试cyper。它可以编译您的 Cython 代码并include_dirs自动设置为 Numpy。

将您的代码加载到一个字符串中,然后简单地运行cymodule = cyper.inline(code_string),然后您的函数cymodule.sparsemaker立即可用。像这样的东西

code = open(your_pyx_file).read()
cymodule = cyper.inline(code)

cymodule.sparsemaker(...)
# do what you want with your function

您可以通过pip install cyper.

于 2020-04-25T05:31:24.347 回答
1

简单的答案

一种更简单的方法是将路径添加到您的文件distutils.cfg。默认情况下,它代表 Windows 7 的路径是C:\Python27\Lib\distutils\. 您只需断言以下内容,它应该可以解决:

[build_ext]
include_dirs= C:\Python27\Lib\site-packages\numpy\core\include

整个配置文件

举个例子,配置文件的样子,我的整个文件如下:

[build]
compiler = mingw32

[build_ext]
include_dirs= C:\Python27\Lib\site-packages\numpy\core\include
compiler = mingw32
于 2015-04-14T12:42:15.023 回答
1

它应该能够在此处cythonize()提到的功能内执行此操作,但它不起作用,因为存在已知问题

于 2019-09-10T07:17:34.417 回答
1

根据这个答案,如果你在 Linux 上安装numpypip,你需要手动设置一个符号链接到/usr/include/numpy

在我的情况下,路径是:

sudo ln -s /usr/local/lib/python3.8/dist-packages/numpy/core/include/numpy/ /usr/include/numpy
于 2021-04-07T11:02:35.683 回答
-1

I hadn't sudo privileges on the server I was running and export CFLAGS didn't work with me. For sake of simplicity, I've installed Anaconda ( https://docs.anaconda.com/anaconda/install/) which creates links to all its installed packages, including Numpy. You can also install miniconda and work with environments to avoid using too much space.

于 2021-11-01T00:10:00.473 回答