'How to call Python from C++?

From the docs :

cppyy is an automatic, run-time, Python-C++ bindings generator, for calling C++ from Python and Python from C++.

(Emphasis mine)

I don't see any instructions for doing the same, however, so is it possible to call Python via C++ using cppyy?



Solution 1:[1]

I'm pretty sure cppyy only allows you to call C++ via Python, not vice versa. You could use Python.h, or use C++ to execute a python file as you would an .exe file, but that would require that the computer can run python files.

Solution 2:[2]

I do not know if it answer to your question (from your title I understand that you want to call python function from c++ side, but later it seems you ask specifically about cpppy) but you can do the binding with pybind11. This package is heavily used and is being used in a lot of case as an alternative to swig (to understand the differences have a look at this open thread in the TensorFlow community). These are the two most used packages for Python-C++ binding.

To see how to call a python function in C++ for Pybind11 have a look at this question, while to see how to call a python function in C++ for swig have a look at this question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 albiremo