Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

c++ call python function

#include <Python.h>
#include <stdlib.h>

int main() {
   // Set PYTHONPATH TO working directory
   setenv("PYTHONPATH",".",1);

   PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *presult;

   // Initialize the Python Interpreter
   Py_Initialize();

   // Build the name object
   pName = PyString_FromString((char*)"arbName");

   // 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, (char*)"someFunction");

   if (PyCallable_Check(pFunc)) {
       pValue=Py_BuildValue("(z)",(char*)"something");
       PyErr_Print();
       printf("Let's give this a shot!
");
       presult=PyObject_CallObject(pFunc,pValue);
       PyErr_Print();
   } 
   else 
       PyErr_Print();
  
   printf("Result is %d
",PyInt_AsLong(presult));
   Py_DECREF(pValue);

   // Clean up
   Py_DECREF(pModule);
   Py_DECREF(pName);

   // Finish the Python Interpreter
   Py_Finalize();
  
   return 0;
}
Comment

c++ call python function

#include <Python.h>
#include <stdlib.h>

int main() {
   // Set PYTHONPATH TO working directory
   setenv("PYTHONPATH",".",1);

   PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *presult;

   // Initialize the Python Interpreter
   Py_Initialize();

   // Build the name object
   pName = PyString_FromString((char*)"arbName");

   // 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, (char*)"someFunction");

   if (PyCallable_Check(pFunc)) {
       pValue=Py_BuildValue("(z)",(char*)"something");
       PyErr_Print();
       printf("Let's give this a shot!
");
       presult=PyObject_CallObject(pFunc,pValue);
       PyErr_Print();
   } 
   else 
       PyErr_Print();
  
   printf("Result is %d
",PyInt_AsLong(presult));
   Py_DECREF(pValue);

   // Clean up
   Py_DECREF(pModule);
   Py_DECREF(pName);

   // Finish the Python Interpreter
   Py_Finalize();
  
   return 0;
}
Comment

PREVIOUS NEXT
Code Example
Python :: pi in python 
Python :: python remove lines of string 
Python :: enumerate from 1 python 
Python :: python dictionary get keys and values 
Python :: python positional argument follows keyword argument 
Python :: import tsv as dataframe python 
Python :: rps python 
Python :: how to use .format in python 
Python :: pandas add value to excel column and save 
Python :: how to run shell command in python 
Python :: python tkinter listbox detect selection change 
Python :: python - regexp to find part of an email address 
Python :: insert row at given position in pandas dataframe 
Python :: Print First 10 natural numbers using while loop 
Python :: how to know if a key is in a dictionary python 
Python :: conda install pypy 
Python :: how to append two numpy arrays 
Python :: python string to list of int 
Python :: list -1 python 
Python :: python trim leading whitespace 
Python :: discord bot delete messages python 
Python :: splitting column values in pandas 
Python :: python advanced programs time module 
Python :: get ip address python 
Python :: blender python add collection to scean collection 
Python :: in python 
Python :: django queryset count 
Python :: python extract substring 
Python :: python help 
Python :: pandas series plot horizontal bar 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =