Help embedding python
Harry George
harry.g.george at boeing.com
Mon Aug 25 15:01:28 EDT 2003
Zora Honey <zhoney at wildmail.com> writes:
> 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
>
I'm no expert but we do have such code working here. We took these
steps:
1. Make a normal "extension" wrapper for at least some of your C++
code. That way the python can call C++ functions and objects. See
Boost, SIP, SWIG, or handcoding for this. (Otherwise python is
blind, deaf, and dumb to the rest of your app.)
2. Embed the python interpreter in your C++ code where it can be
accessed. First, in the startup code (e.g., before calling the
main C++ loop), initialize the interpreter. Second, from wherever
you want to initial a specific python action, hook in the code to
call that interpreter with the needed triggers (e.g., the name of
the python module or function to be called).
3. Because the C++ owns the main loop, and because python interpreter
really ought to own it, we had to do threads and locks to get it
all to work. It was a debugging nightmare for a while.
In other words, this can be done, but it is wrong headed. If we were
to do it again, all of us on the project agree we'd do python as the
main loop (with PyQT in our case), and call C++ extensions as needed
for performance.
--
harry.g.george at boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601
More information about the Python-list
mailing list