<div id=":8b" class="ArwC7c ckChnd"><div>Hi,</div>
<div>I'm trying to use the Python C API but I have</div>
<div>a problem when importing my new module.</div>
<div>Here is my (short) code:<br>#include "Python.h"<br>long sumList(PyObject *list) {<br> int i, n;<br> long total = 0;<br> PyObject *item;<br><br> n = PyList_Size(list);<br> if (n < 0) return NULL;<br>
<br> for (i = 0; i < n; i++) {<br> item = PyList_GetItem(list, i);<br> if (!PyInt_Check(item)) continue;<br> total += PyInt_AsLong(item);<br> }<br> return total;<br>}<br><br>PyObject *wrap_sumList(PyObject *self, PyObject *args) {<br>
PyObject *list;<br> PyObject *total;<br> if (!PyArg_ParseTuple(args, "0&", &list)) {<br> return NULL;<br> }<br> total = Py_BuildValue("l", sumList(list));<br> return total;<br>}<br>
<br>static PyMethodDef exampleMethods[] = {<br> {"sumList", wrap_sumList, METH_O},<br> {NULL}<br>};<br><br>void initexample() {<br> PyObject *m;<br> m = Py_InitModule("example", exampleMethods);<br>
}<br><br>My compilations lines:<br><div id=":8b" class="ArwC7c ckChnd">gcc -Wall -fpic -c -I/usr/include/python2.4 -I/usr/lib/python2.4/config mytest.c<br>gcc -shared mytest.o -o examplemodule.so<br><br>When I try to import examplemodule, I obtain the following message:</div>
</div>
<div>>>> import examplemodule<br>Traceback (most recent call last):<br> File "<stdin>", line 1, in ?<br>ImportError: dynamic module does not define init function (initexamplemodule)<br><br>Someone has an idea ?<br>
Thx,<br>Marc.<br><br></div>
</div>