一尘不染

让distutils在正确的位置查找numpy头文件

python

在我的安装中,numpyarrayobject.h位于…/site- packages/numpy/core/include/numpy/arrayobject.h。我编写了一个使用numpy的普通Cython脚本:

cimport numpy as np

def say_hello_to(name):
    print("Hello %s!" % name)

我还有以下distutils
setup.py(从Cython用户指南中复制):

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

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

setup(
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

当我尝试使用构建时python setup.py build_ext --inplace,Cython尝试执行以下操作:

gcc -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd \
-fno-common -dynamic -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DMACOSX \
-I/usr/include/ffi -DENABLE_DTRACE -arch i386 -arch ppc -pipe \
-I/System/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 \
-c hello.c -o build/temp.macosx-10.5-i386-2.5/hello.o

可以预测,这找不到arrayobject.h。如何使distutils使用numpy包含文件的正确位置(而无需让用户定义$ CFLAGS)?


阅读 147

收藏
2020-12-20

共1个答案

一尘不染

用途numpy.get_include()

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np                           # <---- New line

ext_modules = [Extension("hello", ["hello.pyx"],
                                  include_dirs=[get_numpy_include()])]   # <---- New argument

setup(
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},       
  ext_modules = ext_modules
)
2020-12-20