2010-03-04 20 views
37

Yüklememde, arrayobject.h numarası …/site-packages/numpy/core/include/numpy/arrayobject.h adresinde bulunur.Numpy header dosyalarını doğru yerde arayın.

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 
) 

Ben python setup.py build_ext --inplace oluşturmaya çalıştığımız, Cython yapmaya çalışır:

cimport numpy as np 

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

Ben de (Cython user guide kopyalanan) setup.py aşağıdaki distutils var: Ben numpy kullanan önemsiz Cython senaryoyu yazdım aşağıdaki:

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 

Tahmin edileceği gibi, bu arrayobject.h bulmak için başarısız olur. Ayırt edici dosyaların doğru konumlarını nasıl kullanabilirim (kullanıcı CFLAGS tanımlamaksızın)?

cevap

56

kullanımı numpy.get_include():-ljosa Vebjørn @ verdiği yanıt doğru, ancak

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

Bir ipython defterinde '%% cython' sihirbazını kullanırken aynı problemi yaşıyorum. Bunun için de kolay bir düzeltme olup olmadığını merak ediyorum – pbreach

+1

Bu 'numpy' olmayan bir örnek olarak görünen herkese ilgili iş, "numpy.get_include" tanımı [burada] (https://github.com/numpy/numpy/blob/56678fe56dce97871bb49febf0b2c0206541eada/numpy/lib/utils.py#L18). –

+2

cython 0.24 ile çalışmaya başlamak için 'extension_dirs' satırını 'Extension' çağrısının içine taşımak zorundaydım –

8

install_requires=['numpy'] ile bağlantılı olarak kullanıldığında, bu sorunlara neden olur. Bu durumda, setup.py öğesinin numpy içe aktarması gerekir; bu, ilk olarak pip install numpy'u çalıştırmadan projenize pip install denerseniz bir hataya neden olur.

Projenizin bağımlılığına bağlıysa ve numpy'nin bağımlılık olarak otomatik olarak yüklenmesini isterseniz, include_dirs öğesini yalnızca uzantılarınız gerçekten oluşturulduğunda ayarlamanız gerekir.

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 
) 

Ve Otomatik olarak yüklenen bağımlılık olarak Cython eklemek için benzer bir hile kullanabilirsiniz:: Sen build_ext sınıflara yapabilirsiniz

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 
) 

Not: bu yaklaşımlar sadece pip install . ile çalışır. Bu komutlarda, python setup.py install veya python setup.py develop için çalışmayacaklar, projelerinizden önce, daha önce değil, bağımlılıkların yüklenmesine neden olurlar.