Make distutils look for numpy header files in the correct place

后端 未结 3 674
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 05:44

In my installation, numpy\'s arrayobject.h is located at …/site-packages/numpy/core/include/numpy/arrayobject.h. I wrote a trivial Cython script t

3条回答
  •  余生分开走
    2020-11-28 06:12

    For anyone not using Cython, a slight modification of R_Beagrie's solution without that dependency is if you simply import build_ext from distutils.command.build_ext instead of Cython.

    from distutils.core import setup
    from distutils.extension import Extension
    from distutils.command.build_ext import build_ext
    
    class CustomBuildExtCommand(build_ext):
        """build_ext command for use when numpy headers are needed."""
        def run(self):
    
            # Import numpy here, only when headers are needed
            import numpy
    
            # Add numpy headers to include_dirs
            self.include_dirs.append(numpy.get_include())
    
            # Call original build_ext command
            build_ext.run(self)
    
    ext_modules = [Extension("hello", ["hello.c"])]
    
    setup(
      name = 'Hello world app',
      cmdclass = {'build_ext': CustomBuildExtCommand},
      install_requires=['numpy'],
      ext_modules = ext_modules
    )
    

提交回复
热议问题