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

I am trying to speed up the answer here using Cython. I am trying to compile the code (after executing cygwinccompiler.py hack explained here ), but get the error fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated . Can someone tell me if this is a problem with my code or some esoteric subtlety with Cython?

Below is my code. Thank you in advance:

 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() 
+115
python numpy windows-7 cython
Feb 02 '13 at 0:38
source share
4 answers

In your setup.py , Extension should have an argument include_dirs=[numpy.get_include()] .

In addition, np.import_array() missing in your code.

-

Example 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()] ) 
+163
Feb 02 '13 at 1:26
source share

For a project with one file like yours, another alternative is to use pyximport . You do not need to create setup.py ... you do not even need to open the command line if you use IPython ... all this is very convenient. In your case, try running these commands in IPython or in a regular Python script:

 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 the same for .pyx files, since they work for .py files.

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

+40
Feb 03 '13 at 23:24
source share

An error means that a numpy header file is not created at compile time.

Try export CFLAGS=-I/usr/lib/python2.7/site-packages/numpy/core/include/ and then compile. This is a problem with several different packages. There was an error registered in ArchLinux for the same problem: https://bugs.archlinux.org/task/22326

+11
Feb 02 '13 at 0:46
source share

Simple answer

An easy way is to add the path to your distutils.cfg file. This is the path on behalf of Windows 7 by default C:\Python27\Lib\distutils\ . You simply state the following content and it should work:

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

Entire configuration file

To give you an example of what a configuration file might look like, my entire file reads:

 [build] compiler = mingw32 [build_ext] include_dirs= C:\Python27\Lib\site-packages\numpy\core\include compiler = mingw32 
+1
Apr 14 '15 at 12:42
source share



All Articles