Help embedding python

John Paquin SquatLittleElvis_spammenot at netscape.net
Mon Aug 25 15:32:55 EDT 2003


Zora,

Questions:

1) the docs that come with the python23 runtime have some simple 
examples that may help you.  They go through the process fairly thoroughly

2) What os are you using?

3) what kind of build errors are you getting?  I've always just built 
dynamic libraries built from the source distribution, then linked with 
the python23.lib or python23_d.lib and it's mostly worked.

Here are some things that I've found useful:

if you are defining a new class, it seems that tp_methods (from the 
PyTypeObject class) is not used in the way indicated in the docs.

Instead, I've had to do the following inside my getattr handler:

static PyObject *script_CModel_getattr(PyObject*self,char *attr)
{
return Py_FindMethod(PyMethodDef *pmethods, (PyObject *)self, attr);
}

where pmethods is the same method table you would ordinarily attach to 
tp_methods.

Since the new-style classes have been implemented, it may be necessary 
to subclass new classes from "object" instead of NULL as the docs say.

Finally, to just create a module in C, call:
Py_InitModule("modulename", PyMethodDef *methods);

as the docs indicate.

also, get python to load a .py module, you have to do 2 things:
make sure the path to the module is in the sys.path list, then call 
import_module.
here's how I do it:

PyObject* pSysPath;
PyObject* pmodule;
PyObject* pSysModule = PyImport_AddModule("sys");

if (pSysModule)
{
	pSysPath = PyObject_GetAttrString(pSysModule,"path");


	// note: newpath is a char*
	PyObject *result = PyObject_CallMethod(
					pSysPath,
					"insert",
					"is",0,newpath);

	if (result)
	{
		Py_DECREF(result);

		pmodule = PyImport_ImportModule(module_name);
		result = PyObject_CallMethod(pSysPath,"pop","i",0);
		Py_XDECREF(result);
	}
}

pmodule is now the PyObject version of the module you just loaded.



Sorry if this isn't enough help.



Zora Honey wrote:

> My husband and I are writing a program that does a lot of math behind 
> the scenes (c++) with a gui front (python/Tkinter).  We've decided that 
> we want the c++ to be the "driver", and so we want to embed the python. 
>  We are not stupid people, and we've already searched this newsgroup and 
> the web at large for help, but we're having a heck of a time trying to 
> do this.
> 
> We're using Python 2.2 and Mark Lutz' "Programming Python" as a guide. 
> Unfortunately, the code in there is a little out of date (loading 
> certain libraries), and we haven't been able to get any examples to work 
> (we even tried downloading the version he uses in the book, but it fails 
> on the build).
> 
> So I'm wondering if someone out there has, or would be kind enough to 
> create, a very simple ("hello world" type) program with Makefile that 
> works under 2.2.
> 
> I will be very grateful, as it is my job to do the graphics, so if I 
> can't get python working, I'll have to learn another language...
> 
> Thanks,
> Zora
> 





More information about the Python-list mailing list