Is it possible to execute Python code from C++ without writing to a file?

Roger House rhouse at sonic.net
Fri Apr 15 19:46:54 EDT 2011


I'm a Python newbie who's been given a task requiring calls of Python code
from a C++ program.  I've tried various tutorials and dug into The Python/C
API doc and the Extending and Embedding Python doc, but I haven't been able
to answer this question:

     Is it possible in a C++ program to generate Python code, execute
     it, and get output back (say a tuple of 10 or so items) without
     doing any file i/o?

Clearly it works to write the generated Python to a file and then use
PyImport_Import and PyObject_CallObject to call a function returning the
output tuple.  But it seems like it should be possible to do this without
writing the Python code to a file.  I tried PyRun_String, but I can't 
see how it
can be used to return a tuple (the Py_file_input option always returns 
None).

Any help will be greatly appreciated.

Roger House
Software Developer




More information about the Python-list mailing list