mardi 18 juin 2019

running a python program from c++

I have a python code which runs CNN training/inference. Generally you would run the code with many flags as follow

python MY_PYHTON_FILE train -XX flag1 -YY flag2 ....

I would like to be able to run this program from a c++ code which produces the training sample (and skip the stage where I save the the sample to a file in c++ and read it in python). Is there a way to call the python "main" function and give the parameters which will be stored in argv ?

I am working in a linux OS (ubuntu 18.04 but in the future SL7) using c++ with Python.h header to allow running python

general code I tried for sending parameters to python worked something like this:

        Py_Initialize();

         // Build the name object
        pName = PyString_FromString(argv[1]);
        // Load the module object
        pModule = PyImport_Import(pName);
        // pDict is a borrowed reference 
        pDict = PyModule_GetDict(pModule); 
        // pFunc is also a borrowed reference 
        pFunc = PyDict_GetItemString(pDict, argv[2]);
        if (PyCallable_Check(pFunc))
.
. // prepare pArgs 
.
                pValue = PyObject_CallObject(pFunc, pArgs);
        Py_Finalize();



But in this scenario I need to change my python function to accept arguments. I want the args to go in python's argv

Aucun commentaire:

Enregistrer un commentaire