Reputation: 750
I'm looking for some pointers for the following task: I want to add a Python console to an existing C/C++ program. One important requirement is that the user should be able to access our software through the interpreter. It should be possible to run whole scripts, but also to use the Python interpreter in interactive mode. We already have a Python module by which the user can access our software using sockets, just not integrated into our SW.
Is this possible without embedding and/or extending the Python interpreter? Preferable the user would be able to use whatever python interpreter is already installed. I need the Python interpreter in in interactive mode and then transfer data between the two processes. Is code.InteractiveInterpreter or code.InteractiveConsole (https://docs.python.org/3/library/code.html) the way to go?
Edit: I'm not looking for 3rd-party libraries / tools. I know I can extend the interpreter to get the result.
Either way (extended or not) I'd have to transfer data between the processes. Which kind of inter-process communication would be suitable for this kind of task?
Upvotes: 2
Views: 1490
Reputation: 33
If I understand your question correctly, implementing a Python console can be made simple with pybind11 and embedding the interpreter. Hello world example from docs:
#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::print("Hello, World!"); // use the Python API
}
Types can be converted between Python and C++ objects, and conveniently, the library provides automatic conversion from common standard library types, e.g. std::vector -> list, std::map -> dict, and python objects can be cast.
Upvotes: 1