Make distutils look for numpy header files in the correct place Make distutils look for numpy header files in the correct place python python

Make distutils look for numpy header files in the correct place


Use numpy.get_include():

from distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extimport numpy as np                           # <---- New lineext_modules = [Extension("hello", ["hello.pyx"],                                  include_dirs=[get_numpy_include()])]   # <---- New argumentsetup(  name = 'Hello world app',  cmdclass = {'build_ext': build_ext},         ext_modules = ext_modules)


The answer given by @vebjorn-ljosa is correct, but it causes problems when used in conjunction with install_requires=['numpy']. In this situation, your setup.py needs to import numpy, which will cause an error if you try to pip install your project without running pip install numpy first.

If your project depends on numpy, and you want numpy to be installed automatically as a dependency, you need to set include_dirs only when your extensions are actually being built. You can do this by subclassing build_ext:

from distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extclass 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.pyx"])]setup(  name = 'Hello world app',  cmdclass = {'build_ext': CustomBuildExtCommand},  install_requires=['numpy'],  ext_modules = ext_modules)

And you can use a similar trick to add cython as an automatically installed dependency:

from distutils.core import setupfrom distutils.extension import Extensiontry:    from Cython.setuptools import build_extexcept:    # If we couldn't import Cython, use the normal setuptools    # and look for a pre-compiled .c file instead of a .pyx file    from setuptools.command.build_ext import build_ext    ext_modules = [Extension("hello", ["hello.c"])]else:    # If we successfully imported Cython, look for a .pyx file    ext_modules = [Extension("hello", ["hello.pyx"])]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)setup(  name = 'Hello world app',  cmdclass = {'build_ext': CustomBuildExtCommand},  install_requires=['cython', 'numpy'],  ext_modules = ext_modules)

Note: these approaches only work with pip install .. They won't work for python setup.py install or python setup.py develop as in these commands cause dependencies to be installed after your project, rather than before.


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 setupfrom distutils.extension import Extensionfrom distutils.command.build_ext import build_extclass 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)