C ++ embedded Python
Add the include/libs directory of python to include/lib directories of VC respectively. In addition, Python does not provide debug Lib. Specifically, Python does not provide python25_d.lib. You can compile PythonSource codeTo obtain python25_d.lib. So, if you want to runProgramYou need to change pyconfig. H (in the python25/include/directory) To The Pragma comment (Lib, "python32_d.lib") in line 283.
Comment (Lib, "python32.lib"), so that python uses non-Debug Lib.
# Include <Python/python. h> // contains the header file and embedding python in C ++. This is required. </P> <p> int main () <br/>{< br/> // call the initialization operation before python <br/> py_initialize (); </P> <p> pyobject * pmodule = NULL; <br/> pyobject * pfunc = NULL; </P> <p> // import script <br/> pmodule = pyimport_importmodule ("Gmail "); </P> <p> // obtain the function from the import module <br/> pfunc = pyobject_getattrstring (pmodule, "hello "); </P> <p> // call pyeval_callobject to execute your function <br/> pyeval_callobject (pfunc, null); </P> <p> py_finalize (); </P> <p> return 0; <br/>}< br/>
An example with a parameter
Python File
# Filename test2.py <br/> def Hello (s): <br/> Print "Hello, world! "<Br/> Print s
CPP File
# Include <python. h> <br/> int main () <br/>{< br/> py_initialize (); <br/> pyobject * pmodule = NULL; <br/> pyobject * pfunc = NULL; <br/> pyobject * parg = NULL; <br/> pmodule = pyimport_importmodule ("Test2 "); <br/> pfunc = pyobject_getattrstring (pmodule, "hello"); <br/> parg = py_buildvalue ("(s)", "functionwith argument "); <br/> pyeval_callobject (pfunc, parg); <br/> py_finalize (); <br/> return0; <br/>}
Send an email using Python and C ++ mixed programming:
// Gmail. PY <br/> Import smtplib <br/> fromemail. mime. text import mimetext </P> <p> defsendemail (STR ): <br/> fromaddr = 'from @ gmail.com '<br/> toaddrs = 'to @ 163.com' <br/> MSG = mimetext (STR) <br/> MSG ['subobject'] = 'I miss you' <br/> MSG ['from'] = 'from @ gmail.com' <br/> MSG [' '] =' to @ 163.com '<br/> # credentials (if needed) <br/> username = 'from' <br/> Password = 'Password' <br/> # The actual Mail Send <br/> Server = smtplib. SMTP ('smtp .gmail.com: 100') <br/> server. starttls () <br/> server. login (username, password) <br/> server. sendmail (fromaddr, toaddrs, MSG. as_string () <br/> server. quit () </P> <p> A = 'hacky. gray'
// main. CPP <br/> # include <Python/python. h> // contains the header file and embedding python in C ++. This is required. </P> <p> int main () <br/>{< br/> // call the initialization operation before python <br/> py_initialize (); </P> <p> pyobject * pmodule = NULL; <br/> pyobject * pfunc = NULL; <br/> pyobject * parg = NULL; </P> <p> // import script <br/> pmodule = pyimport_importmodule ("Gmail "); </P> <p> // obtain the function from the import module <br/> pfunc = pyobject_g Etattrstring (pmodule, "sendemail"); </P> <p> // construct a string <br/> parg = py_buildvalue ("(s )", "You have done a good job! "); // Construct a tuples </P> <p> // call pyeval_callobject to execute your function <br/> pyeval_callobject (pfunc, parg ); </P> <p> py_finalize (); </P> <p> system ("pause"); </P> <p> return 0; <br/>}
In addition, if no statement can be directly executed in The py file, an error occurs.