Python C + + mixed programming

Source: Internet
Author: User
Tags gettext gtk mixed python script
 
Python pdf book:http://www.oschina.net/bbs/thread/9780 Python's mixed programming with C or C + +: include calling APIs in C + + in Python, and calling Python scripts in C + +. Build Python and C + + Hybrid systems: http://blog.csdn.net/mythma/archive/2009/09/15/4556155.aspx Run the Hello World program in Python: http:// Hi.baidu.com/rongjch/blog/item/1f548851e078cb8e8d543032.html/cmtid/5e9f7b600ef93dd18cb10d6a how to invoke the Python method in C + +: http://bambooice.blog.hexun.com/37655834_d.html #include The "Python.h" compilation option, you need to manually specify Python include paths, and link paths, code: g++ Py thonconsole.cpp-i/usr/local/include/python2.7-l/usr/local/lib/python2.7-lpython2.7 C language Editing method gcc emu.c-l/usr/lib/ python2.2/config-lpython2.2-lpthread-lm-ldl-lutil/usr/local/include/python2.7//usr/local/lib/python2.7//usr/lo cal/share/man/man1/python2.7.1 C + + How to invoke the meaning of invoking a Python script in the Python method C + + is not to say, at least you can take it as a text form of the dynamic link library, when needed can also be changed, as long as the interface does not change, C + + program once compiled, and then change is not so convenient to see the code code of Python: #test function def add (a,b): print "In Python function add" prin T "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 the above Python code is pytest.py Next is C + + code code: #include "Python.h" int ma In (int argc, char** argv) {//Initialize Python//before using the Python system, you must initialize it with a py_initialize. It loads the Python built-in module and adds the system path//path to the module search paths. 

    This function does not return a value, check whether the system//is initialized successfully requires the use of py_isinitialized. 

    Py_initialize (); Check if initialization is successful if (! 
    Py_isinitialized ()) {return-1; ///Add the current path//run the input string directly as Python code, return 0//indicates success,-1 indicates error. 
    Most of the time the error is due to a syntax error in the string//. 
    pyrun_simplestring ("Import sys"); 
    Pyrun_simplestring ("Sys.path.append (';/')"); 

    Pyobject *pname,*pmodule,*pdict,*pfunc,*pargs; 
    Load the 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 into stack *pargs; 

    PArgs = Pytuple_new (2); 
    pyobject* Py_buildvalue (char *format, ...) Convert a C + + variable to a Python object. This function is used when you need to pass variables to Python from//C + +. This function//is somewhat similar to C's printf, but the format is different. 

    The commonly used format has//s to denote 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)); 

    Call Python function Pyobject_callobject (Pfunc, PArgs); 
    The following section is Lookup 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 include paths, and link paths, code: g++ pythontest.cpp-i/usr/local/include/python2.7-l/usr/local/lib/p Ython2.7-lpython2.7-lpthread-ldl-lm-lutil If your Python version number is different from mine, please revise your version number to the original address: Http://coredump.blog.bokee.net/blo ggermodule/blog_printentry.do?id=909014 Reference: 1,python Chinese Community: http://python.cn//usr/local/include/python2.7//usr/loc al/lib/python2.7//usr/local/share/man/man1/python2.7.1/usr/bin$ ls python*-l lrwxrwxrwx 1 root root 9 2010-10-2 1 16:20 python-> python2.6 lrwxrwxrwx 1 root 9 2010-10-21 16:20 python2-> python2.6-rwxr-xr-x 1 root R Oot 2230352 2009-10-20 11:48 python2.6/usr/bin$ which python/usr/local/bin/python import GTK importerror:no mod Ule namedgtk/usr/lib/python2.5/site-packages/usr/local/lib/python2.7/site-packages/usr/local/lib/python2.6/  Site-packages This directory exists the 3rd party packages can is installed here.

Read the source for site.py to more details. site.py installation, pay attention to GTK selected >>> import sys >>> print sys.path [', '/usr/local/lib/python27.zip ', '/u sr/local/lib/python2.7 ', '/usr/local/lib/python2.7/plat-linux2 ', '/usr/local/lib/python2.7/lib-tk ', '/usr/local/ Lib/python2.7/lib-old ', '/usr/local/lib/python2.7/lib-dynload ', '/usr/local/lib/python2.7/site-packages '] > >> print Sys.path [', '/usr/lib/python2.6 ', '/usr/lib/python2.6/plat-linux2 ', '/usr/lib/python2.6/lib-tk ', '/ Usr/lib/python2.6/lib-old ', '/usr/lib/python2.6/lib-dynload ', '/usr/lib/python2.6/dist-packages ', '/usr/lib/ Python2.6/dist-packages/pil ', '/usr/lib/python2.6/dist-packages/gst-0.10 ', '/usr/lib/pymodules/python2.6 ', '/usr/ lib/python2.6/dist-packages/gtk-2.0 ', '/usr/lib/pymodules/python2.6/gtk-2.0 ', '/Usr/local/lib/python2.6/dist-packages '] >>> shows Python's site-packages path: $ python-c "from Distutils.sysconfig Import Get_python_lib; Print get_python_lib () "/usr/local/lib/python2.7/site-packages Configuring additional built-in modules--------------  -------------------------starting with Python 2.1, the setup.py script in the top of the "source distribution attempts to  Detect which modules can be built and automatically compiles. Autodetection doesn ' t always work, so you can still customize the configuration to the editing file;  But this should is considered a last resort.
The rest of this section is only applies if your decide to edit the Modules/setup file.




You are also need this to enable static linking of certain modules (which are needed to enable profiling on some systems).
After manually compiling and installing Python, IBUs cannot use my manual compilation to install python2.6, after which IBUs will not be able to use it. The following traceback (most recent call last) appears: File "/usr/share/ibus/setup/main.py", line <module> import GTK Imp OrterroR:no module named GTK but python2.5.4 have been all right, said, will not be new to compile Python, all related to the software to recompile it.
Well, it seems to be. At least Python modules have to be recompiled, like pygtk or something. Yes, recompile python, you must compile all the Python modules.

To compile PYGTK, you must compile a large pile of dependencies. 

So, generally, if you just want a python of your own, compile it into a different directory from the system and rename the Python Master program as a solution. Install PYGTK on Linux to install PYGTK packs on Linux for Python 2.6.5 as required by the project.

  Originally thought very simple one thing, or use Easy_install installation, or the source code compiles installs, is very simple; did not want to make a day, Leng did not fix, during the trouble, the installation file depends on the problem. First of all, it is not feasible to use Easy_install in the installation pygtk.

  Because not only to compile C code, but also to detect machine conditions, you must be compiled in a regular source code to install, such as configure, make, make install, and so on. In addition, installation PYGTK need to rely on the pygobject, which depends on the gobject-introspection, and to install Gobject-introspection, you must install glib, installation GLib may install upgrade gettext, Because the old version of GetText does not recognize some of the new methods in glib. In addition, the installation of these packages will require that the version number should not be less than so-and-so.

  So, the complexity, installation, uninstall, eventually failed. Just looked at the PYGTK official website, it seems to be able to install under Windows.

This weekend to find a time to study again, and strive to install a successful pygtk. 

 Python-gobject

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.