2010-03-04 97 views
37

在我的安裝中,numpy的arrayobject.h位於…/site-packages/numpy/core/include/numpy/arrayobject.h。我寫了一個使用numpy的一個簡單的用Cython腳本:讓distutils在正確的位置尋找numpy頭文件

cimport numpy as np 

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

我也有以下的distutils setup.py(從Cython user guide複製):

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)?

回答

56

使用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"])] 

setup(
    name = 'Hello world app', 
    cmdclass = {'build_ext': build_ext}, 
    include_dirs = [np.get_include()],   # <---- New line 
    ext_modules = ext_modules 
) 
+2

我在ipython筆記本中使用'%% cython'魔法時遇到同樣的問題..我想知道是否有一個簡單的解決方案 – pbreach 2014-09-14 06:26:32

+1

對於任何人看這個作爲非'numpy'相關工作,'numpy.get_include'的定義是[here](https://github.com/numpy/numpy/blob/56678fe56dce97871bb49febf0b2c0206541eada/numpy/lib/utils.py#L18)。 – 2015-07-07 04:30:19

+2

我不得不在「Extension」調用中移動「include_dirs」行,以使其與cython 0.24一起工作 – 2016-06-08 19:19:21

8

通過@給vebjorn-ljosa的答案是正確的,但連同install_requires=['numpy']使用時會引起問題。在這種情況下,您的setup.py需要導入numpy,如果您先嚐試pip install項目而不運行pip install numpy,則會導致錯誤。

如果您的項目依賴於numpy,並且您希望numpy作爲依賴項自動安裝,則只有在實際構建擴展時才需要設置include_dirs。您可以通過繼承build_ext做到這一點:

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

class CustomBuildExtCommand(build_ext): 
    """build_ext command for use when numpy headers are needed.""" 
    def run(self): 

     # Import numpy here, only when headers are needed 
     import numpy 

     # Add numpy headers to include_dirs 
     self.include_dirs.append(numpy.get_include()) 

     # Call original build_ext command 
     build_ext.run(self) 

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

setup(
    name = 'Hello world app', 
    cmdclass = {'build_ext': CustomBuildExtCommand}, 
    install_requires=['numpy'], 
    ext_modules = ext_modules 
) 

而且你可以使用類似的技巧,用Cython添加爲自動安裝依賴:

from distutils.core import setup 
from distutils.extension import Extension 

try: 
    from Cython.setuptools import build_ext 
except: 
    # If we couldn't import Cython, use the normal setuptools 
    # and look for a pre-compiled .c file instead of a .pyx file 
    from setuptools.command.build_ext import build_ext 
    ext_modules = [Extension("hello", ["hello.c"])] 
else: 
    # If we successfully imported Cython, look for a .pyx file 
    ext_modules = [Extension("hello", ["hello.pyx"])] 

class CustomBuildExtCommand(build_ext): 
    """build_ext command for use when numpy headers are needed.""" 
    def run(self): 

     # Import numpy here, only when headers are needed 
     import numpy 

     # Add numpy headers to include_dirs 
     self.include_dirs.append(numpy.get_include()) 

     # Call original build_ext command 
     build_ext.run(self) 

setup(
    name = 'Hello world app', 
    cmdclass = {'build_ext': CustomBuildExtCommand}, 
    install_requires=['cython', 'numpy'], 
    ext_modules = ext_modules 
) 

注:這些方法只能用pip install .工作。它們將不適用於python setup.py installpython setup.py develop,因爲這些命令會導致依賴項安裝在項目之後,而不是之前。