I would like to gather multiple Python modules under one package, so they do not reserve too many names from global set of python packages and modules. But I have problems with modules that are written in C.
Here is a very simple example, straight from the official Python documentation. You can find it at the bottom of the page from here: http://docs.python.org/distutils/examples.html
from distutils.core import setup
from distutils.extension import Extension
setup(name='foobar',
version='1.0',
ext_modules=[Extension('foopkg.foo', ['foo.c'])],
)
My foo.c file looks like this
#include <Python.h>
static PyObject *
foo_bar(PyObject *self, PyObject *args);
static PyMethodDef FooMethods[] = {
{
"bar",
foo_bar,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};
static PyObject *
foo_bar(PyObject *self, PyObject *args)
{
return Py_BuildValue("s", "foobar");
}
PyMODINIT_FUNC
initfoo(void)
{
(void)Py_InitModule("foo", FooMethods);
}
int
main(int argc, char *argv[])
{
// Pass argv[0] to the Python interpreter
Py_SetProgramName(argv[0]);
// Initialize the Python interpreter. Required.
Py_Initialize();
// Add a static module
initfoo();
return 0;
}
It builds and installs fine, but I cannot import foopkg.foo! If I rename it to just "foo" it works perfectly.
Any ideas how I can make the "foopkg.foo" work? For example changing "foo" from Py_InitModule() in C code to "foopkg.foo" does not help.