How to make a *.pyd or a DLL of several modules?

Adrian Eyre a.eyre at optichrome.com
Wed Mar 15 14:55:48 CET 2000


> I have several Python modules and want to put them into a PYD or 
> into a DLL. Freezing only supports executeables, so how can i make
> one DLL of these modules? Importing that DLL should be the same
> like importing all Modules.

It's fairly straightforward to import all modules into sys.modules,
you just call Py_InitModule() for each module.

Putting them into the namespace of the import call is a bit more
tricky though.

Try something like this (works for me on win32):

#include <Python.h>
#include <stdio.h>

static PyObject* foo_meth1(PyObject* self, PyObject* args)
{
	printf("foo_meth1\n");
	Py_INCREF(Py_None);
	return Py_None;
}
static PyObject* foo_meth2(PyObject* self, PyObject* args)
{
	printf("foo_meth2\n");
	Py_INCREF(Py_None);
	return Py_None;
}
static PyObject* bar_meth1(PyObject* self, PyObject* args)
{
	printf("bar_meth1\n");
	Py_INCREF(Py_None);
	return Py_None;
}
static PyObject* bar_meth2(PyObject* self, PyObject* args)
{
	printf("bar_meth2\n");
	Py_INCREF(Py_None);
	return Py_None;
}

static PyMethodDef foo_methods[] = {
	{ "meth1", foo_meth1, METH_VARARGS },
	{ "meth2", foo_meth2, METH_VARARGS },
	{ NULL, NULL }
};

static PyMethodDef bar_methods[] = {
	{ "meth1", bar_meth1, METH_VARARGS },
	{ "meth2", bar_meth2, METH_VARARGS },
	{ NULL, NULL }
};

void _declspec(dllexport) initfoo()
{
	PyObject* locals = PyEval_GetLocals();
	PyObject* barmod;	

	Py_InitModule("foo", foo_methods);	/* This one is done for you */
	barmod = Py_InitModule("bar", bar_methods);
	PyDict_SetItemString(locals, "bar", barmod);
	Py_INCREF(barmod);
}





More information about the Python-list mailing list