I have a C++-program that allows me to run Python-scripts that C++ passes data to. If the Python-script fails the C++-program calls PyRun_InteractiveLoop to allow investigating the problem on an interactive shell. This works alright but I thought "Would be cool if IPython is installed I could use it in that case".
Now my problem is: all I find when looking for "ipython embedding" is instructions how to embed IPython into Python-programs (amongst others http://ipython.org/ipython-doc/dev/interactive/reference.html#embedding-ipython). I tried to reproduce these on the embedded regular python-shell I have but most of them fail in some way (usually because of a missing sys.argv .... that one I can solve).
Any suggestions how to do this? My first plan would have been to first import IPython via the C-API (that one I got covered). If that fails use the "regular" shell. Otherwise call IPython.embed() (or similar) via PyRun_InteractiveOneFlags
You can try importing IPython with PyImport_ImportModule, and then getting the embed function with PyObject_GetAttrString. Once you have the embed function, you can call it with PyObject_CallObject to start IPython's interactive shell.
Here is some example code for how you might do this:
PyObject *ipy_module = PyImport_ImportModule("IPython"); if (ipy_module == NULL) { // IPython not installed, fall back to regular shellPyRun_InteractiveOneFlags(...); } else { PyObject *embed_func = PyObject_GetAttrString(ipy_module, "embed"); if (embed_func == NULL) { // Error getting embed function, fall back to regular shellPyRun_InteractiveOneFlags(...); } else { PyObject_CallObject(embed_func, NULL); } Py_XDECREF(embed_func); } Py_XDECREF(ipy_module);
Keep in mind that this is just an example and you might need to adjust it to suit your specific use case. Also, you might have to handle errors and exceptions that are raised when calling PyImport_ImportModule and PyObject_GetAttrString .