0

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.

3
  • Can you try: ext_modules = cythonize((Extension("test", sources=["test.pyx"], include_dirs=[np.get_include()], ), )) Commented Jun 21, 2017 at 15:34
  • Also, what is the version of cython? cython3 --version Commented Jun 21, 2017 at 15:34
  • Pierre, that's magic! It works, although I don't know why/how (yet) Commented Jun 21, 2017 at 15:35

2 Answers 2

3

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".

Sign up to request clarification or add additional context in comments.

Comments

1

My workaround:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.