H
H Linux
Dear all,
I am currently fighting with a problem writing a set of Python
extensions in C. I want to structure the whole package (here called
smt for sub-module test) into different sub-modules, e.g. according to
this layout:
smt.foo.func()
I can only build a module
Once I try to nest this, I cannot get the module to load anymore:
File "<stdin>", line 1, in <module>
ImportError: No module named bar
I have found the following hints on the web:
http://stackoverflow.com/questions/1681281/nested-python-c-extensions-modules
I have also found that XEN has a python module which does this, but
http://www.google.de/codesearch#4Wqoij9clTg/tools/python/setup.py
Still I am unable to get this to work. What am I missing? In case it
matters, this is on Ubuntu10.4/Python2.6.5 Below are listings from my
source files.
Thanks in advance for any help,
Hartwig
1. setup.py:
from distutils.core import setup, Extension
PACKAGE_NAME = 'smt'
setup(
name = PACKAGE_NAME,
version = '0.1',
author = 'Myself',
packages = [PACKAGE_NAME],
ext_modules = [ Extension('foo', ['src/foo.c']),
Extension('smt.bar', ['src/bar.c']) ]
)
2. src/bar.c
#include <Python.h>
static PyObject*
bar_func(PyObject *self, PyObject *args)
{
PyObject *a, *b;
if (!PyArg_UnpackTuple(args, "func", 2, 2, &a, &b)) {
return NULL;
}
return PyNumber_Add(a, b);
}
static PyMethodDef bar_methods[] = {
{"func", bar_func, METH_VARARGS, NULL},
{NULL, NULL}
};
PyMODINIT_FUNC
initbar(void)
{
Py_InitModule("smt.bar", bar_methods);
}
3. src/foo.c
#include <Python.h>
static PyObject*
foo_func(PyObject *self, PyObject *args)
{
PyObject *a, *b;
if (!PyArg_UnpackTuple(args, "func", 2, 2, &a, &b)) {
return NULL;
}
return PyNumber_Add(a, b);
}
static PyMethodDef foo_methods[] = {
{"func", foo_func, METH_VARARGS, NULL},
{NULL, NULL}
};
PyMODINIT_FUNC
initfoo(void)
{
Py_InitModule("foo", foo_methods);
}
4. Code Layout:
├── setup.py
├── smt
│  └── __init__.py
└── src
├── bar.c
└── foo.c
I am currently fighting with a problem writing a set of Python
extensions in C. I want to structure the whole package (here called
smt for sub-module test) into different sub-modules, e.g. according to
this layout:
smt.foo.func()
I can only build a module
import foo
print foo.func(1,2)
Once I try to nest this, I cannot get the module to load anymore:
Traceback (most recent call last):import smt.bar
File "<stdin>", line 1, in <module>
ImportError: No module named bar
I have found the following hints on the web:
http://stackoverflow.com/questions/1681281/nested-python-c-extensions-modules
I have also found that XEN has a python module which does this, but
http://www.google.de/codesearch#4Wqoij9clTg/tools/python/setup.py
Still I am unable to get this to work. What am I missing? In case it
matters, this is on Ubuntu10.4/Python2.6.5 Below are listings from my
source files.
Thanks in advance for any help,
Hartwig
1. setup.py:
from distutils.core import setup, Extension
PACKAGE_NAME = 'smt'
setup(
name = PACKAGE_NAME,
version = '0.1',
author = 'Myself',
packages = [PACKAGE_NAME],
ext_modules = [ Extension('foo', ['src/foo.c']),
Extension('smt.bar', ['src/bar.c']) ]
)
2. src/bar.c
#include <Python.h>
static PyObject*
bar_func(PyObject *self, PyObject *args)
{
PyObject *a, *b;
if (!PyArg_UnpackTuple(args, "func", 2, 2, &a, &b)) {
return NULL;
}
return PyNumber_Add(a, b);
}
static PyMethodDef bar_methods[] = {
{"func", bar_func, METH_VARARGS, NULL},
{NULL, NULL}
};
PyMODINIT_FUNC
initbar(void)
{
Py_InitModule("smt.bar", bar_methods);
}
3. src/foo.c
#include <Python.h>
static PyObject*
foo_func(PyObject *self, PyObject *args)
{
PyObject *a, *b;
if (!PyArg_UnpackTuple(args, "func", 2, 2, &a, &b)) {
return NULL;
}
return PyNumber_Add(a, b);
}
static PyMethodDef foo_methods[] = {
{"func", foo_func, METH_VARARGS, NULL},
{NULL, NULL}
};
PyMODINIT_FUNC
initfoo(void)
{
Py_InitModule("foo", foo_methods);
}
4. Code Layout:
├── setup.py
├── smt
│  └── __init__.py
└── src
├── bar.c
└── foo.c