如何在setup.py中引导numpy安装 [英] How to Bootstrap numpy installation in setup.py

查看:180
本文介绍了如何在setup.py中引导numpy安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,它的C扩展名需要numpy.理想情况下,我希望任何下载我的项目的人都能够运行python setup.py install或使用一次对pip的调用.我的问题是,在我的setup.py中,我需要导入numpy以获取标头的位置,但是我希望numpy成为install_requires中的常规要求,以便它可以从Python自动下载包裹索引.

I have a project which has a C extension which requires numpy. Ideally, I'd like whoever downloads my project to just be able to run python setup.py install or use one call to pip. The problem I have is that in my setup.py I need to import numpy to get the location of the headers, but I'd like numpy to be just a regular requirement in install_requires so that it will automatically be downloaded from the Python Package Index.

以下是我要执行的操作的一个示例:

Here is a sample of what I'm trying to do:

from setuptools import setup, Extension
import numpy as np

ext_modules = [Extension('vme', ['vme.c'], extra_link_args=['-lvme'],
                         include_dirs=[np.get_include()])]

setup(name='vme',
      version='0.1',
      description='Module for communicating over VME with CAEN digitizers.',
      ext_modules=ext_modules,
      install_requires=['numpy','pyzmq', 'Sphinx'])

很显然,在安装它之前,我不能在顶部import numpy.我已经看到传递给setup()setup_requires参数,但是找不到有关其用途的任何文档.

Obviously, I can't import numpy at the top before it's installed. I've seen a setup_requires argument passed to setup() but can't find any documentation on what it is for.

这可能吗?

推荐答案

以下内容至少适用于numpy1.8和python {2.6,2.7,3.3}:

The following works at least with numpy1.8 and python{2.6,2.7,3.3}:

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

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(
    ...
    cmdclass={'build_ext':build_ext},
    setup_requires=['numpy'],
    ...
)

要作一个简短的解释,请参阅为什么没有"hack"就失败,请参见此答案.

For a small explanation, see why it fails without the "hack", see this answer.

请注意,使用setup_requires有一个细微的缺点:例如,numpy不仅会在构建扩展之前进行编译,还会在进行python setup.py --help时进行编译.为了避免这种情况,您可以检查命令行选项,如 https:中建议的那样: //github.com/scipy/scipy/blob/master/setup.py#L205 ,但另一方面,我并不认为这样做值得.

Note, that using setup_requires has a subtle downside: numpy will not only be compiled before building extensions, but also when doing python setup.py --help, for example. To avoid this, you could check for command line options, like suggested in https://github.com/scipy/scipy/blob/master/setup.py#L205, but on the other hand I don't really think it's worth the effort.

这篇关于如何在setup.py中引导numpy安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆