Bart
Bart

Reputation: 10250

Cython with `cimport numpy` fails

Similar questions have been asked many, many times, but unfortunately I have yet again ran into a problem with using Cython with Numpy. Take this minimal example (which pretty much follows the examples here):

# file test.pyx
import numpy as np
#cimport numpy as np

def func():
    print("hello")

Which I try to build with:

from distutils.core import setup
from Cython.Build import cythonize
import numpy as np
import os

os.environ["CC"] = "g++-7"

setup(
    ext_modules = cythonize("test.pyx", include_path = [np.get_include()])
)

This example works (python setup.py build_ext --inplace), until I un-comment the cimport ... line, after which I get the well know error:

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

The path returned by np.get_include() does have the arrayobject.h header, but in the actual g++ command that gets executed, the include dir is missing as a -I/...:

g++-7 -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -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/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c test.c -o build/temp.macosx-10.11-x86_64-3.6/test.o

Any idea what might cause this problem?

I'm using Python 3.6.1 on Mac OS, everything (Cython, Numpy, ..) installed with pip3, and Cython 0.25.2.

Upvotes: 0

Views: 2737

Answers (2)

Li-chih Wu
Li-chih Wu

Reputation: 1082

My workaround:

os.environ["C_INCLUDE_PATH"] = np.get_include()
setup(
    ext_modules = cythonize("test.pyx")
)

Upvotes: 1

Pierre de Buyl
Pierre de Buyl

Reputation: 7293

Instead of the simple cythonize command, use

ext_modules = cythonize((Extension("test", sources=["test.pyx"], include_dirs=[np.get_include()], ), ))

The include_dirs option is given here to "Extension" instead of using include_path with "cythonize".

Upvotes: 3

Related Questions