Building and installing the extension module

Once we have written the functions successfully, the next thing to do is build the module and use it in our Python modules. The setup.py file looks like the following code snippet:

from distutils.core import setup, Extension 
import numpy 
# define the extension module 
demo_module = Extension('numpy_api_demo', sources=['numpy_api.c'], 
include_dirs=[numpy.get_include()]) 
 
# run the setup 
setup(ext_modules=[demo_module]) 

As we are using NumPy-specific headers, we need to have the numpy.get_include function in the include_dirs variable. To run this setup file, we will use a familiar command:

python setup.py build_ext -inplace 

The preceding command will create a numpy_api_demo.pyd file in the directory for us ...

Get NumPy Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.