When using the C language, we sometimes encounter the need to call Python functions to complete some specific functions. Next, we will introduce in detail how C calls Python functions and hope to help you.
Python script, saved as pytest. py
- def add(a,b):
- print "in python function add"
- print "a = " + str(a)
- print "b = " + str(b)
- print "ret = " + str(a+b)
- return a + b
Example code for C to call a Python function:
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include "C:/Python26/include/python. h"
- # Pragma comment (lib, "C :\\ Python26 \ libs \ python26.lib ")
- Int main (int argc, char ** argv)
- {
- // Initialize Python
- // Before using the Python system, you must use Py_Initialize
- // Perform initialization. It loads Python built-in modules and adds the system path.
- // Path to the module search path. This function has no return value. Check the system
- // Use Py_IsInitialized to check whether the initialization is successful.
- PyObject * pName, * pModule, * pDict, * pFunc, * pArgs, * pRetVal;
- Py_Initialize ();
- // Check whether Initialization is successful
- If (! Py_IsInitialized ())
- {
- Return-1;
- }
- // Load the script named pytest (Note: It is not pytest. py)
- PName = PyString_FromString ("pytest ");
- PModule = PyImport_Import (pName );
- If (! PModule)
- {
- Printf ("can't find pytest. py ");
- Getchar ();
- Return-1;
- }
- PDict = PyModule_GetDict (pModule );
- If (! PDict)
- {
- Return-1;
- }
- // Find the function named add
- PFunc = PyDict_GetItemString (pDict, "add ");
- If (! PFunc |! PyCallable_Check (pFunc ))
- {
- Printf ("can't find function [add]");
- Getchar ();
- Return-1;
- }
- // Stack parameters
- PArgs = PyTuple_New (2 );
- // PyObject * Py_BuildValue (char * format ,...)
- // Convert the C ++ variable into a Python object. When you need
- // This function is used when C ++ passes a variable to Python. This function
- // A bit similar to C's printf, but the format is different. Common formats include:
- // S indicates a string,
- // I indicates an integer variable,
- // F indicates a floating point number,
- // O indicates a Python object.
- PyTuple_SetItem (pArgs, 0, Py_BuildValue ("l", 3 ));
- PyTuple_SetItem (pArgs, 1, Py_BuildValue ("l", 4 ));
- // Call the Python Function
- PRetVal = PyObject_CallObject (pFunc, pArgs );
- Printf ("function return value: % ld \ r \ n", PyInt_AsLong (pRetVal ));
- Py_DECREF (pName );
- Py_DECREF (pArgs );
- Py_DECREF (pModule );
- Py_DECREF (pRetVal );
- // Close Python
- Py_Finalize ();
- Return 0;
- }
- // Another method for personal practice
- # Include <Python. h>
- # Include <conio. h>
- Int main ()
- {
- Py_Initialize ();
- If (! Py_IsInitialized ())
- {
- Printf ("initialization error \ n ");
- Return-1;
- }
- PyObject * pModule = NULL;
- PyObject * pFunc = NULL;
- PyObject * pArg = NULL;
- PyObject * pRetVal = NULL;
- PModule = PyImport_ImportModule ("hello ");
- PFunc = PyObject_GetAttrString (pModule, "hello ");
- PArg = Py_BuildValue ("(I, I)", 33,44 );
- PRetVal = PyObject_CallObject (pFunc, pArg );
- Printf ("% d \ n", PyInt_AsLong (pRetVal ));
- Py_Finalize ();
- _ Getch ();
- Return 0;
- }
The above is an introduction to the operation methods related to calling Python functions in C.