繁体   English   中英

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

[英]How to handle dependency on scipy in setup.py

我正在尝试为依赖于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。

最终,这对我有用:

#!/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',
    ],
    ...
)

使用setup_requires参数来安装numpy之前的scipy

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

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

brew install gcc

ps如果你有AttributeError: 'module' object has no attribute 'get_include'看看为什么setup_requires没有正常工作numpy? 所以问题,它假设解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM