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

发布于 2021-01-29 19:28:21

在我的安装中,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)?

关注者
0
被浏览
45
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    用途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
    )
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看