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 to use in the Python interpreter.
To test our module, we will open a Python interpreter test, and try to call these functions from the module exactly like we do for a module written in Python:
>>>import numpy_api_demo as npd >>> import numpy as np >>>npd.py_square_func(4) >>> 16.0 >>> x = np.arange(0,10,1) >>> y = npd.np_square(x)
3.146.176.88