2

I am trying to move my Ipython notebook code to python. But I have the error

fatal error: 'numpy/arrayobject.h' file not found
#include "numpy/arrayobject.h"

, even though I have included numpy in the setup

My setup.py:

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

setup(
    ext_modules=cythonize("Trajectory.pyx"),
    include_dirs=[numpy.get_include()]
)

the Trajectory.pyx file

cimport numpy as np
import  numpy as np 

I am running on osX, Python 2.7.10

It also gives me this information before the error, hope this help identifying the issue: clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c Trajectory.c -o build/temp.macosx-10.11-x86_64-2.7/Trajectory.o

And when I ran

import numpy
numpy.get_include()

I get:

'/usr/local/lib/python2.7/site-packages/numpy/core/include'

And I look into the directory, /numpy/arrayobject.h is there. So I really don't know why it said no such a file

J_yang
  • 2,672
  • 8
  • 32
  • 61
  • The [documentation](http://cython.readthedocs.io/en/latest/src/reference/compilation.html) suggests that you should pass `include_path` to `cythonize` or `include_dirs` to the `Extension` class. You seem to be passing `include_dirs` to `setup` which presumably isn't recognised. – DavidW Aug 24 '16 at 06:06
  • Hi, Thanks. Now I got it work in a very weird fashion. I do ext_modules=cythonize("Trajectory.pyx") to generate Trajectory.c first, with the same error above. Than, recompile with ext_modules=[ Extension("Trajectory", ["Trajectory.c"], include_dirs=[numpy.get_include()]),]. To generate the .so..... Any idea how to correct this? – J_yang Aug 24 '16 at 07:49
  • 2
    I think you should either do `ext_modules = cythonize("Trajectory.pyx",include_path=[np...])` or do `ext_modules=cythonize([Extension(...,include_dirs=[np...])])` – DavidW Aug 24 '16 at 08:17
  • yes. That is what I did... But I have to use cythonize to generate the .c file first, then the extension (and commenting out the cythinize bit) to get the final .so. I got the same error if I just use either of them. – J_yang Aug 24 '16 at 08:47
  • 3
    For some reason, both include_dirs and include_parth won't get added properly. I used os.environ at the end which solve the issue: os.environ["CPPFLAGS"] = os.getenv("CPPFLAGS", "") + "-I" + numpy.get_include() – J_yang Aug 24 '16 at 11:51

1 Answers1

3

FWIW, I ran into the same issue ('numpy/arrayobject.h' file not found) on macOS (Python 3.7.6, Cython 0.29.14, macOS 10.15).

This is the workaround I used to get the include path right:

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

setup(name='Foo',
      ext_modules=cythonize([Extension("bar", ["bar.pyx"], include_dirs=[numpy.get_include()])])
)
Rob van der Leek
  • 1,486
  • 15
  • 13
  • There is no point in putting `include_dirs` into setup, because it is not a recognized setup-keyword: https://github.com/python/cpython/blob/e42b705188271da108de42b55d9344642170aa2b/Lib/distutils/core.py#L42. It should be done as proposed by DavidW in his comment: `ext_modules=cythonize([Extension(...,include_dirs=[np...])])`, Setting `include_path` in cythonize has a different meaning see https://stackoverflow.com/a/56817889/5769463 – ead Mar 05 '20 at 13:48
  • This answer is wrong https://stackoverflow.com/a/2379912/5769463, but comments show what should be done. – ead Mar 05 '20 at 13:50