Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I embed Python in a C++ Builder / Delphi 2010 application?

I'm interested in experimenting with embedding Python in my application, to let the user run Python scripts within the application environment, accessing internal (C++-implemented) objects, etc. I'm quite new to this so don't know exactly what I'm doing.

I have read Embedding Python in Another Application, though this seems to talk only about a C API and flat C functions, not classes or objects (unless I've missed something) and its "Embedding Python in C++" section is only two sentences long. However, I also came across how to use boost::python and this looks excellent.

There's one problem: boost::python is not supported by C++ Builder 2010.

So, given this, what is the best approach for embedding Python in a C++ application compiled with C++ Builder 2010, and, using whichever technique is best, how do you expose / integrate classes and objects to give the Python coder access to the object-oriented internals of a program? Have I missed a standard approach? Is exposing internal classes or instantiated objects to Python as objects easy, or is the API truly C-style or flat / non-OO, and if so what's the best approach to mimic an underlying OO layer through such an API?

Note: I actually use RAD Studio, which includes both C++ Builder and Delphi. It may be possible to make use of some sort of Delphi-specific binding, but the ones I've encountered are six or seven years old, or are new-ish (Python 2.6) but don't seem to have any documentation and have comments in the issue list like "Anyone reads thiese [sic] comments anyway? Anyone working on this project?" which is not encouraging. But please feel free to include Delphi-specific answers especially if you think it's likely they'll work in a combined D+CB app. I appreciate all answers even if they aren't quite perfect - I can research, I just need pointers on where to go. A native C++ solution would probably be ideal, though, since using VCL-derived objects has its own limitations.

Thanks for your input!

like image 643
David Avatar asked Aug 10 '10 07:08

David


People also ask

Why is Python embeddable?

Embedding provides your application with the ability to implement some of the functionality of your application in Python rather than C or C++. This can be used for many purposes; one example would be to allow users to tailor the application to their needs by writing some scripts in Python.


1 Answers

You should not be afraid of the P4D project at google groups. It seems inactive because, in part, it is very stable and full-featured already. Those components are used in the much more active PyScripter application which is one of the best python development editors currently available. PyScripter is written in Delphi and uses the P4D components. As such, it also presents a very comprehensive example of how to use the P4D components, although the examples provided with the P4D source checkout are already good enough to get started.

like image 156
Caleb Hattingh Avatar answered Oct 02 '22 14:10

Caleb Hattingh