2010-03-04 15 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 साथ बनाने की कोशिश, Cython करने की कोशिश करता:

cimport numpy as np 

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

मैं भी निम्नलिखित distutils setup.py (Cython user guide से नकल) है: मैं एक छोटी सी Cython स्क्रिप्ट का उपयोग करता है 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 के सही स्थान का उपयोग फ़ाइलों को कैसे शामिल कर सकता हूं (उपयोगकर्ता को $ 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

आईपीथॉन नोटबुक में '%% साइथन' जादू का उपयोग करते समय मुझे एक ही समस्या है .. मुझे आश्चर्य है कि इसके लिए – pbreach

+1

के लिए कोई आसान फिक्स है, क्योंकि इसे गैर-'अम्पी' संबंधित कार्य, 'numpy.get_include' की परिभाषा [यहां] है (https://github.com/numpy/numpy/blob/56678fe56dce97871bb49febf0b2c0206541eada/numpy/lib/utils.py#L18)। –

+2

मुझे 'एक्सटेंशन' कॉल के अंदर 'include_dirs' पंक्ति को स्थानांतरित करना था ताकि इसे साइथन 0.24 के साथ काम करने के लिए प्राप्त किया जा सके। –

8

जवाब-ljosa vebjorn @ द्वारा दिए गए सही है, लेकिन यह जब install_requires=['numpy'] के संयोजन में किया समस्याओं का कारण बनता। इस स्थिति में, आपके setup.py को numpy आयात करने की आवश्यकता है, जो आपके प्रोजेक्ट को pip install numpy चलाने के बिना पहले कोई त्रुटि उत्पन्न करेगा।

यदि आपकी परियोजना numpy पर निर्भर करती है, और आप निर्भरता के रूप में स्वचालित रूप से स्थापित होने के लिए numpy चाहते हैं, तो आपको केवल एक्सटेंशन_डिर्स सेट करना होगा जब आपके एक्सटेंशन वास्तव में बनाए जा रहे हों। आप 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 install या python setup.py develop के लिए काम नहीं करेंगे क्योंकि इन आदेशों में आपकी प्रोजेक्ट के बाद पहले की बजाय निर्भरता स्थापित की जा सकती है।