Подтвердить что ты не робот

Сделать distutils искать файлы заголовков numpy в правильном месте

В моей установке numpy arrayobject.h находится в …/site-packages/numpy/core/include/numpy/arrayobject.h. Я написал тривиальный Cython script, который использует numpy:

cimport numpy as np

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

У меня также есть следующие distutils setup.py (скопировано из Руководство пользователя Cython):

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

4b9b3361

Ответ 1

Используйте 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

Ответ, данный @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 install или python setup.py develop, как в этих командах, потому что зависимости устанавливаются после вашего проекта, а не раньше.