7

I have a scenario where I have some functions in C++ classes and I want to be able to call them using a python script. Let's say I have a function

void greet(_msg);
    std::cout >> _msg >> std::endl;

I want to be able to call it trough a custom Python call and pass arguments to it, for example using

saySomething("Hello")

As a .py file I want it to call the greet function and pass "Hello" as an argument.

I know it's a subject that has been throughly discussed, and I've done a share of research on embedding python in C++, I've managed to read values from a python script using the standard Python/C API and run a function in Python from C++ and pass argument to it, but I can't seem to get my head around how to achieve this specific outcome.

I've had a look at ctypes and various wrappin libraries such as boost:python or swig, but I can't seem to understand to which degree they could help me achieve want I want.

leoncvlt
  • 385
  • 2
  • 5
  • 13
  • "run a function in Python from C++" what do you mean with that ? If you like to mask C/C++ as Python API you can apply the basic rules of the Python bindings for C/C++, there is also the boost::python library that can be easier to handle when writing python API starting from C++ code. – user1797612 Nov 21 '12 at 17:25
  • Sorry it wasn't clear - What I want is being able to have a python script that calls a specific function of a C++ class being able to pass arguments to it. More or less like a GUI, but done through python scripts instead of buttons. – leoncvlt Nov 21 '12 at 18:03
  • so you are looking for this http://docs.python.org/3.2/extending/extending.html , or boost::python ( probably easier ), and also the answers to this question can be a good starting point http://stackoverflow.com/questions/276761/exposing-a-c-api-to-python . Anyway, you want to generate python api for C++ code, that's it, just follow the official guide or use this libs. – user1797612 Nov 21 '12 at 18:07

3 Answers3

5

Depending on which version of Python you are interested in, 2.x or 3.x, read through the Extending and Embedding the Python Interpreter chapter for 2.x or 3.x. You are interested only in extending Python, so section the 1. Extending Python with C or C++ will provide you with complete explanation how to implement what you need in order to be able to call your functions implemented in C++ from Python script.

Certainly, there are numerous libraries and generators which allow you to wrap C/C++ APIs for Python (e.g. Boost.Python or SWIG), but your case sounds simple enough, that for the purpose of learning it is IMO better to get familiar with Python C API. Even if you use these tools, you will frequently have to get down to Python C API anyway or at least understand it.

mloskot
  • 37,086
  • 11
  • 109
  • 136
4

I recently needed to do this very thing. Boost.Python does what we're looking for (and more) but personally (as much as I love Boost) I find it a little overkill to have to drag in half the Boost library to get one feature. SWIG also wasn't really an option for me as code generation always becomes a pain to maintain while class structures change (Don't get me wrong, these are BRILLIANT solutions!, just not what I was looking for).

So, the only thing left for me was to implement it from first principles (Python/C API). Hense, "ECS:Python" was born. ECS:Python (Embedded C++ Scripting with Python) is a simple C++ Python wrapper library I designed specifically for C++ developers. It allows you to expose objects from a C++ application to an embedded Python interpreter for interactive scripting, and it's very light-weight and easy to use.

Its free (BSD) and open source. If you're interested here it is: http://sourceforge.net/projects/ecspython

3

You can use the weave.inline() function, which is part of the scipy package, to compile and execute C/C++ files and get their output from within your python script.

SidR
  • 2,964
  • 1
  • 18
  • 32