使distutils在正确的位置查找numpy头文件

50
在我的安装中,numpy的arrayobject.h位于.../site-packages/numpy/core/include/numpy/arrayobject.h。我编写了一个使用numpy的微不足道的Cython脚本:
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)?

3个回答

73

使用 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"],
                                  include_dirs=[get_numpy_include()])]   # <---- New argument

setup(
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},       
  ext_modules = ext_modules
)

2
当我在IPython笔记本中使用%%cython魔法时,我遇到了同样的问题。我想知道是否也有一个简单的解决方法。 - pbreach
2
对于任何想要查看此示例以进行非 numpy 相关工作的人,numpy.get_include 的定义在这里 - 0 _
1
我不确定我是否正确地实现了它,但似乎这使得对于远程安装无效。您会收到一个错误信息 ModuleNotFoundError: No module named 'numpy' - Erik K
1
这个可以工作,但是无法发布到pypi,因为我没有安装numpy包! - Jingpeng Wu
6
get_numpy_include() 应该改为 np.get_include() 吗? - Demitri
显示剩余3条评论

32
@vebjorn-ljosa提供的答案是正确的,但是在与install_requires=['numpy']一起使用时会出现问题。在这种情况下,您的setup.py需要导入numpy,如果您尝试在没有先运行pip install numpy的情况下pip install您的项目,则会导致错误。
如果您的项目依赖于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,因为这些命令会导致依赖项在您的项目之后安装,而不是在之前。

distutils已被弃用。 - user3673

0

对于不使用Cython的任何人,没有这个依赖项的R_Beagrie解决方案的轻微修改是,如果你只是从distutils.command.build_ext导入build_ext。

from distutils.core import setup
from distutils.extension import Extension
from distutils.command.build_ext 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.c"])]

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

1
distutils已被弃用。 - user3673

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接