How to Bootstrap numpy installation in setup.py

前端 未结 7 1112
陌清茗
陌清茗 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:52

    Perhaps a more practical solution is to just require numpy to be installed beforehand and import numpy inside a function scope. @coldfix solution works but compiling numpy takes forever. Much faster to pip install it first as a wheels package, especially now that we have wheels for most systems thanks to efforts like manylinux.

    from __future__ import print_function
    
    import sys
    import textwrap
    import pkg_resources
    
    from setuptools import setup, Extension
    
    
    def is_installed(requirement):
        try:
            pkg_resources.require(requirement)
        except pkg_resources.ResolutionError:
            return False
        else:
            return True
    
    if not is_installed('numpy>=1.11.0'):
        print(textwrap.dedent("""
                Error: numpy needs to be installed first. You can install it via:
    
                $ pip install numpy
                """), file=sys.stderr)
        exit(1)
    
    def ext_modules():
        import numpy as np
    
        some_extention = Extension(..., include_dirs=[np.get_include()])
    
        return [some_extention]
    
    setup(
        ext_modules=ext_modules(),
    )
    

提交回复
热议问题