2

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?

3
  • It's too broad. Python interpreter allows use any OS object to communicate between processes, so those two processes shouldn't be related anyhow. From another point you can use boost.Python or just native Python API to create a process in C++, which will call python scripts or any other internal thing. Furthermore, you can take source code of Python console and add there whatever you want to add for any type of communication. Commented Nov 18, 2018 at 14:31
  • Interprocess communication is specific to OS. I would suggest to use zmq.inproc sockets if you want a nice and strong multilanguage abstraction for it. Commented Nov 18, 2018 at 14:32
  • 1
    @Arkady: Thank you for your comment. I should add I don't want to use 3rd party libraries - if not absolutely necessary. Anyway, my question is more about the "interactive python interpreter" and only secondarily about the communication between our software and the interpreter. Commented Nov 18, 2018 at 14:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

pybind11 seems to be similar to "Boost.Python", a library helping you to embed / extend the python interpreter. My question is do I really need to embed and extend the python interpreter? Preferable the user would be able to use whatever python interpreter is already installed.
You would target the Python interpreter you have already installed in your system, there is no need for a separate installation. Simply find_package(PythonLibs) in your cmake file. As others have mentioned, IPC with e.g. zmq is another possibility. If you stick with something like msgpack for data interchange, type conversion should also be pretty straightforward.
This would still be for a embedded solution, and while useful for me, I'm looking for a non-embedded solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.