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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14657375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:05:15  来源:igfitidea点击:

Cython: "fatal error: numpy/arrayobject.h: No such file or directory"

pythonwindows-7numpycython

提问by Noob Saibot

I'm trying to speed up the answer hereusing Cython. I try to compile the code (after doing the cygwinccompiler.pyhack explained here), but get a fatal error: numpy/arrayobject.h: No such file or directory...compilation terminatederror. Can anyone tell me if it's a problem with my code, or some esoteric subtlety with Cython?

我正在尝试使用 Cython加快这里的答案。我尝试编译代码(在执行此处cygwinccompiler.py解释的hack之后),但出现错误。谁能告诉我是我的代码有问题,还是 Cython 有一些深奥的微妙之处?fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated

Below is my code.

下面是我的代码。

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()

采纳答案by Robert Kern

In your setup.py, the Extensionshould have the argument include_dirs=[numpy.get_include()].

在你的setup.pyExtension应该有论点include_dirs=[numpy.get_include()]

Also, you are missing np.import_array()in your code.

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

--

——

Example setup.py:

示例 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()]
)    

回答by John Brodie

The error means that a numpy header file isn't being found during compilation.

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

Try doing export CFLAGS=-I/usr/lib/python2.7/site-packages/numpy/core/include/, and then compiling. This is a problem with a few different packages. There's a bug filed in ArchLinux for the same issue: https://bugs.archlinux.org/task/22326

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

回答by Steve Byrnes

For a one-file project like yours, another alternative is to use pyximport. You don't need to create a setup.py... you don't need to even open a command line if you use IPython ... it's all very convenient. In your case, try running these commands in IPython or in a normal Python script:

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

You may need to edit the compiler of course. This makes import and reload work the same for .pyxfiles as they work for .pyfiles.

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

Source: http://wiki.cython.org/InstallingOnWindows

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

回答by strpeter

Simple answer

简单的回答

A way simpler way is to add the path to your file distutils.cfg. It's path behalf of Windows 7 is by default C:\Python27\Lib\distutils\. You just assert the following contents and it should work out:

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

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

Entire config file

整个配置文件

To give you an example how the config file could look like, my entire file reads:

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

[build]
compiler = mingw32

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

回答by hsc

It should be able to do it within cythonize()function as mentioned here, but it doesn't work beacuse there is a known issue

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

回答by Syrtis Major

If you are too lazy to write setup files and figure out the path for include directories, try cyper. It can compile your Cython code and set include_dirsfor Numpy automatically.

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

Load your code into a string, then simply run cymodule = cyper.inline(code_string), then your function is available as cymodule.sparsemakerinstantaneously. Something like this

将您的代码加载到一个字符串中,然后简单地运行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

You can install cyper via pip install cyper.

您可以通过pip install cyper.