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

50 投票
3 回答
22849 浏览
提问于 2025-04-15 20:00

在我的安装环境中,numpy的 arrayobject.h 文件位于 …/site-packages/numpy/core/include/numpy/arrayobject.h。我写了一个简单的Cython脚本来使用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)?

3 个回答

0

如果你不想使用Cython,可以稍微修改一下R_Beagrie的解决方案,只需从distutils.command.build_ext导入build_ext,而不是使用Cython。

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
)
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,因为这两个命令会在你的项目之后安装依赖项,而不是之前。

74

使用 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
)

撰写回答