Answers for "c++ embed python"

C++
0

c++ embed python

//Link to an article meant for humans if the Python doc makes your head spin
//https://medium.datadriveninvestor.com/how-to-quickly-embed-python-in-your-c-application-23c19694813

//Python
def test(person):
    return "What's up " + person;

//C++
//Run a python function
PyObject *pName, *pModule, *pFunc, *pArgs, *pValue;
pName = PyUnicode_FromString((char*)"script");				  // Script name
pModule = PyImport_Import(pName);							  
pFunc = PyObject_GetAttrString(pModule, (char*)"test");		  // Function name
pArgs = PyTuple_Pack(1, PyUnicode_FromString((char*)"Greg")); // Parameter
pValue = PyObject_CallObject(pFunc, pArgs);
auto result = _PyUnicode_AsString(pValue);					  // Return
std::cout << result << std::endl;

// Output - What's up Greg
Posted by: Guest on October-25-2021

Browse Popular Code Answers by Language