2010-03-04 5 views
37

설치시 numpy의 arrayobject.h…/site-packages/numpy/core/include/numpy/arrayobject.h에 있습니다.distutils가 정확한 위치에 numpy 헤더 파일을 찾도록하십시오.

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 빌드하려고, 사이 썬이해야 할 시도합니다

cimport numpy as np 

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

나는 또한합니다 (Cython user guide에서 복사) setup.py는 다음의 distutils이 : 나는 NumPy와 사용 사소한 사이 썬 스크립트를 작성 다음과 같습니다 :

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 include 파일의 정확한 위치를 사용하게 할 수 있습니까? (사용자가 $ CFLAGS를 정의하지 않고)?

답변

56

사용 numpy.get_include() :-ljosa vebjorn @에 의해 주어진 대답은 정확하지만

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

+1

'-numpy'가 아닌 예를 들어 본 사람은 누구입니까? 'numpy.get_include'의 정의는 [여기] (https://github.com/numpy/numpy/blob/56678fe56dce97871bb49febf0b2c0206541eada/numpy/lib/utils.py#L18)입니다. –

+2

'Extension' 호출 안에'include_dirs' 행을 이동 시켜서 cython 0.24와 함께 작동 시키도록했습니다. –

8

install_requires=['numpy']와 함께 사용할 때 문제가 발생합니다. 이 경우, pip install numpy을 먼저 실행하지 않고 pip install 프로젝트를 시도하면 setup.py가 numpy를 가져와야합니다. 그러면 오류가 발생합니다.

프로젝트가 numpy에 종속되고 numpy가 종속성으로 자동 설치되도록하려면 확장이 실제로 빌드 될 때만 include_dirs를 설정해야합니다.

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 
) 

을 그리고 당신은 자동으로 설치 종속성으로 사이 썬 추가 비슷한 트릭을 사용할 수 있습니다 : 당신은 build_ext를 서브 클래 싱하여이 작업을 수행 할 수 있습니다

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 install 또는 python setup.py develop으로 작동하지 않으므로 종속성이 이전보다 프로젝트 이후에 설치됩니다.

관련 문제