How to Bootstrap numpy installation in setup.py

前端 未结 7 1087
陌清茗
陌清茗 2020-12-02 12:12

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

7条回答
  •  隐瞒了意图╮
    2020-12-02 12:55

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

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

    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.

提交回复
热议问题