C language call PYTHON script-2

Source: Internet
Author: User

C language call PYTHON script-2python Code 2: call. pydef test (): print 'Hello world' def add (a, B): return a + B api. pyimport io def load_test (): fp = open ('call. py', 'R') buffer = ''if fp: buffer = fp. read () fp. close () return buffer cpp Code: # include <stdio. h> # include <stdlib. h> # include <Python. h> int main (int argc, char * argv []) {Py_Initialize (); if (! Py_IsInitialized () {return-1;} PyRun_SimpleString ("import sys"); PyRun_SimpleString ("sys. path. append ('. /') "); PyObject * pName; PyObject * pModule; PyObject * pDict; PyObject * pFunc; pName = PyString_FromString (" api "); pModule = PyImport_Import (pName ); if (! PModule) {printf ("can't find call. py"); getchar (); return-1;} pDict = PyModule_GetDict (pModule); if (! PDict) {return-1 ;}{ pFunc = PyDict_GetItemString (pDict, "load_test"); if (! PFunc |! PyCallable_Check (pFunc) {printf ("can't find function [test]"); getchar (); return-1;} PyObject * pFn = PyObject_CallObject (pFunc, 0 ); char * buffer = PyString_AsString (pFn); printf ("% s \ n", buffer); PyObject * o = Py_CompileString (buffer, "none", Py_file_input ); pyObject * m = PyImport_ExecCodeModule (". a ", o); PyObject * d = PyModule_GetDict (m); pFunc = PyDict_GetItemString (d," add "); if (! PFunc |! PyCallable_Check (pFunc) {printf ("can't find function [add]"); getchar (); return-1;} PyObject * args = PyTuple_New (2 ); values (args, 0, Py_BuildValue ("l", 3); PyTuple_SetItem (args, 1, Py_BuildValue ("l", 4); PyObject * pAdded = PyObject_CallObject (pFunc, args); int ret = PyInt_AsLong (pAdded); printf ("add value: % d \ n", ret) ;}py_finalize (); system ("PAUSE "); return 0;} the code is a little different from the previous one. The main difference is that the python module is loaded from the memory and the main part of the function is called: PyObject * o = Py_CompileString (buffer, "none ", py_file_input); PyObject * m = PyImport_ExecCodeModule (". a ", o); PyObject * d = PyModule_GetDict (m); buffer is a python source code string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.