如何在setup.py中处理对scipy的依赖

时间:2014-11-19 15:57:34

标签: python python-2.7 scipy setuptools

我正在尝试为依赖于SciPy的项目创建setup.py。以下setup.py重现了这一点:

setup(
    name='test',
    version='0.1',
    install_requires=['scipy']
)

使用python setup.py develop安装时会产生以下错误:

ImportError: No module named numpy.distutils.core

然而,当我使用pip安装scipy时,它是从一个轮子安装它,它工作得很好。

所以,我的问题是,如何创建依赖于SciPy的setup.py?为什么不会setuptools从轮子安装依赖项?使用Python 3时这会更好吗(我们计划无论如何都要迁移,所以如果它在那里工作,我会等到迁移完成后)。

我在Mac OS X 10.10.1上使用Python 2.7.8 setuptools 3.6和pip 1.5.6。

2 个答案:

答案 0 :(得分:2)

最终,这对我有用:

#!/usr/bin/env python

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as _build_ext

#
# This cludge is necessary for horrible reasons: see comment below and
# http://stackoverflow.com/q/19919905/447288
#
class build_ext(_build_ext):
    def finalize_options(self):
        _build_ext.finalize_options(self)
        # Prevent numpy from thinking it is still in its setup process:
        __builtins__.__NUMPY_SETUP__ = False
        import numpy
        self.include_dirs.append(numpy.get_include())

setup(
    #
    # Amazingly, `pip install scipy` fails if `numpy` is not already installed.
    # Since we cannot control the order that dependencies are installed via
    # `install_requires`, use `setup_requires` to ensure that `numpy` is available
    # before `scipy` is installed.
    #
    # Unfortunately, this is *still* not sufficient: `numpy` has a guard to
    # check when it is in its setup process that we must circumvent with
    # the `cmdclass`.
    #
    setup_requires=['numpy'],
    cmdclass={'build_ext':build_ext},
    install_requires=[
        'numpy',
        'scipy',
    ],
    ...
)

答案 1 :(得分:2)

使用setup_requires参数在numpy之前安装scipy

setup(
    name='test',
    version='0.1',
    setup_requires=['numpy'],
    install_requires=['scipy']
)

注意:还需要Fortran编译器来构建scipy。您可以通过brew安装它:

brew install gcc

p.s。如果您AttributeError: 'module' object has no attribute 'get_include'查看Why doesn't setup_requires work properly for numpy? SO问题,则可以解决该问题。