为什么setup_requires对于numpy无法正常工作? [英] Why doesn't setup_requires work properly for numpy?

查看:108
本文介绍了为什么setup_requires对于numpy无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个setup.py文件,该文件自动将构建时依赖关系解析为numpy(用于编译扩展名).我的第一个猜测是使用setup_requires并子类化一个命令类来导入numpy模块:

I wanted to create a setup.py file that automatically resolves a build-time dependency to numpy (for compiling extensions). My first guess was to use setup_requires and subclass a command class to import the numpy module:

from setuptools import setup, Extension
from distutils.command.build import build as _build

class build(_build):
    def run(self):
        import numpy
        print(numpy.get_include())
        _build.run(self)

setup(
    name='test',
    version='0.0',
    description='something',
    cmdclass={'build':build},
    setup_requires=['numpy'],
)

现在,运行python setup.py build成功编译了numpy,但随后失败了(在build.run内部):

Now, running python setup.py build successfully compiles numpy but then fails (inside build.run) with:

AttributeError: 'module' object has no attribute 'get_include'

但是,如果再次运行相同的命令,则该命令现在可以成功(并且不需要重新编译numpy).

However, if the running the same command again, the command now succeeds (and doesn't need to recompile numpy).

我已经在python {2.6,2.7,3.3}上使用最新版本的setuptools在带有和不带有virtualenv的情况下对此进行了测试.

I have tested this on python{2.6,2.7,3.3} with and without virtualenv on pretty recent versions setuptools.

我已经看到使用pkg_resources.resource_filename 解决方法似乎可以正常工作,如果我们想要的只是include目录. 编辑:仅适用于python2!

I have seen a workaround using pkg_resources.resource_filename which seems to work just fine, if all we want is the include directory. EDIT: only works on python2!

但是,我现在仍然很好奇. setup_requires的用法有哪些警告?对于numpy无法正常工作的原因可能是什么?对于一些更简单的模块,似乎没有问题.

But still, I am now curious. What caveats does the usage of setup_requires have? What could be the reasons that it doesn't work properly for numpy? For some more simple modules it seems to have no problems.

推荐答案

指出,通过检查numpy/__init__.py内部的__NUMPY_SETUP__来阻止numpy模块的正确初始化:

Figured out, that a proper initialization of the numpy module is prevented by a check for __NUMPY_SETUP__ inside numpy/__init__.py:

if __NUMPY_SETUP__:
    import sys as _sys
    _sys.stderr.write('Running from numpy source directory.\n')
    del _sys
else:
    # import subodules etc. (main branch)

安装后,setuptools不会重置此全局状态.以下作品:

This global state is not reset by setuptools after the installation. The following works:

...
def run(self):
    __builtins__.__NUMPY_SETUP__ = False
    import numpy
    ...

这篇关于为什么setup_requires对于numpy无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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