48

私のインストールでは、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もあります( Cythonユーザーガイドsetup.pyからコピー):

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 を定義させずに)。

4

3 に答える 3

72

使用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
)
于 2010-03-04T14:19:16.507 に答える