User guide

NumPy User Guide, Release 1.9.0
/
*
This builds the answer back into a python object
*
/
return Py_BuildValue("d", p);
}
/
*
This initiates the module using the above definitions.
*
/
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"spam",
NULL,
-1,
SpamMethods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC PyInit_spam(void)
{
PyObject
*
m;
m = PyModule_Create(&moduledef);
if (!m) {
return NULL;
}
return m;
}
#else
PyMODINIT_FUNC initspam(void)
{
PyObject
*
m;
m = Py_InitModule("spam", SpamMethods);
if (m == NULL) {
return;
}
}
#endif
To use the setup.py file, place setup.py and spammodule.c in the same folder. Then python setup.py build will build
the module to import, or setup.py install will install the module to your site-packages directory.
’’’
setup.py file for spammodule.c
Calling
$python setup.py build_ext --inplace
will build the extension library in the current file.
Calling
$python setup.py build
will build a file that looks like ./build/lib
*
, where
lib
*
is a file that begins with lib. The library will
be in this file and end with a C library extension,
such as .so
Calling
$python setup.py install
82 Chapter 5. Using Numpy C-API