Python – Cython: fatal error: ‘numpy/arrayobject.h’ file not found, using numpy

cythoncythonizenumpypython

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

Best Answer

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()])])
)
Related Topic