How to Bootstrap numpy installation in setup.py

前端 未结 7 1114
陌清茗
陌清茗 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条回答
  •  旧时难觅i
    2020-12-02 13:08

    The key is to defer importing numpy until after it has been installed. A trick I learned from this pybind11 example is to import numpy in the __str__ method of a helper class (get_numpy_include below).

    from setuptools import setup, Extension
    
    class get_numpy_include(object):
        """Defer numpy.get_include() until after numpy is installed."""
    
        def __str__(self):
            import numpy
            return numpy.get_include()
    
    
    ext_modules = [Extension('vme', ['vme.c'], extra_link_args=['-lvme'],
                             include_dirs=[get_numpy_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'])
    

提交回复
热议问题