How Python is called in C/

Source: Internet
Author: User
Tags python script

The meaning of calling a Python script in C + + is no, at least you can think of it as a dynamic link library in textual form,

Need time can also change a change, as long as not change the interface, C + + program once compiled, and then change is not so convenient

Look at Python code first.
Code:

#test function

def add (A, B):

print "In Python function add"

print "a =" + str (a)

print "b =" + str (b)

print "ret =" + str (A+B)

Return



def foo (a):

print "In Python function foo"

print "a =" + str (a)

print "ret =" + str (A * a)

Return



Save the Python code above as pytest.py

Next comes the code for C + +
Code:

#include "Python.h"

int main (int argc, char** argv)
{
Initialize Python
Before using a Python system, you must use Py_initialize to
To initialize. It will load the Python built-in module and add the system path
To the module search path. This function does not return a value, check the system
The success of initialization requires the use of py_isinitialized.

Py_initialize ();

Check if initialization is successful
if (! Py_isinitialized ())
{
return-1;
}

Add Current Path
Runs the input string directly as Python code, returning 0
Indicates success, 1 indicates an error. Most of the time the error is due to the string
There is a syntax error in the.
pyrun_simplestring ("Import sys");
Pyrun_simplestring ("Sys.path.append ('./')");
Pyobject *pname,*pmodule,*pdict,*pfunc,*pargs;

Load a script named Pytest
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;
}

parameter in stack
*pargs;
PArgs = Pytuple_new (2);

pyobject* Py_buildvalue (char *format, ...)
Convert C + + variables into a Python object. When needed from
This function is used when C + + passes variables to python. This function
A bit similar to C's printf, but in a different format. The commonly used formats are
s represents a String,
I represents an integer variable,
F represents a floating-point number,
O represents a Python object.

Pytuple_setitem (PArgs, 0, Py_buildvalue ("L", 3));
Pytuple_setitem (PArgs, 1, Py_buildvalue ("L", 4));

Calling the Python function
Pyobject_callobject (PFunc, PArgs);

The following is the Find function foo and execute foo
PFunc = pydict_getitemstring (pdict, "foo");
if (!pfunc | |!) Pycallable_check (PFunc))
{
printf ("Can ' t find function [foo]");
GetChar ();
return-1;
}

PArgs = pytuple_new (1);
Pytuple_setitem (PArgs, 0, Py_buildvalue ("L", 2)); //

Pyobject_callobject (PFunc, PArgs);


Py_decref (PName);
Py_decref (PArgs);
Py_decref (Pmodule);

Close Python
Py_finalize ();
return 0;
}


Compile options, you need to manually specify Python's include path, and link path,


g++ Python.cpp-o python-i/usr/include/python2.5-l/usr/lib/python2.5-lpython2.5

How Python is called in C/

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.