Bind C++ program for use with both Python 2.x and 3.x

Stefan Behnel stefan_ml at behnel.de
Sun Dec 12 06:06:28 EST 2010


Peter C., 11.12.2010 23:41:
> Hello, I am looking at the possibility of making a program in C++. The
> catch is it will require the ability to work with binding for use with
> scripting in both Python 2.x and 3.x for various tool plugins.

Do I read this right that you want your program to be written in C++, and 
use an embedded Python interpreter to provide scripting capabilities?

Is there a reason you'd want to write the application itself in C++ in the 
first place? If it's new code, it's likely faster to write it Python 
directly. Usually, that also leads to a lot less code, which helps in 
keeping the code more maintainable.


> Is
> there any way to bind a C++ app to work with both Python 2.x and 3.x
> using the Python C API? Note if I could I'd just do Python 3, however
> I need Python 2 support to allow for the use of this application as a
> plugin in apps that use Python 2 as well.

The easiest (and fastest etc.) way to do that is to write the glue code in 
Cython. It generates portable C/C++ code that supports Python 2.3 to 3.2. 
However, if you ship binaries, you will still need to provide them for the 
different Python versions (even for different Py2.x versions), as the 
runtimes are not binary compatible.

Stefan




More information about the Python-list mailing list