0

I have a code that uses numpy and I want to compile it using Cython. I added the cimport directive:

import numpy as np
cimport numpy as np

I am on Windows 7, compiling using distutils with gcc (MinGW) as the compiler. It yields an error when I try to compile it. This is the error:

ssepMC.c:346:31: fatal error: numpy/arrayobject.h: No such file or directory
 #include "numpy/arrayobject.h"
                               ^
compilation terminated.
error: command 'gcc' failed with exit status 1

I believe this error occurs because the compiler is trying to compile the numpy package. But this is an unnecessary step because a compiled version of numpy exists in Cython under

C:\Python27\Lib\site-packages\Cython\Includes\numpy\numpy.pxd

So the question is: How do I make the compiler use the compiled version of numpy?

Thanks in advance.

Avision
  • 3,613
  • 1
  • 19
  • 24
  • 2
    This looks like a duplicate of this [other question](http://stackoverflow.com/questions/2379898/make-distutils-look-for-numpy-header-files-in-the-correct-place). – IanH Apr 18 '14 at 01:02
  • Thanks @IanH, this seems to solve it. However if there's a way to not compile numpy, but use the precompiled version, it would be cool – Avision Apr 18 '14 at 09:32
  • 2
    I'm not familiar enough with NumPy's internals to say which bits of NumPy this is re-compiling. I doubt it will be an issue, but I could be wrong. Really though, in practice, if you don't want to deal with this dependency, [memory views](http://docs.cython.org/src/userguide/memoryviews.html) are the easiest route to take. – IanH Apr 18 '14 at 17:02
  • I'll have a look at it. Thanks again :-) – Avision Apr 19 '14 at 14:37

1 Answers1

-1

As mentioned elsewhere, you need to point gcc to numpy/arrayobject.h which you can do by setting include_dirs to [np.get_include()].

Note - this is not 'recompiling' numpy, this is using the directives in the numpy arrayheader file to instruct gcc how to compile your code.

from Cython.Build import cythonize
import numpy as np

ext_modules = [Extension("hello", ["hello.pyx"], include_dirs=[np.get_include()])]

setup(
  ext_modules = cythonize(extensions)
)
danodonovan
  • 19,636
  • 10
  • 70
  • 78