Use Python library from C++

Discussion in 'Python' started by Chang LI, Nov 25, 2004.

  1. Chang LI

    Chang LI Guest

    I want to use the Python Libray from my C++ program.
    The Python Libray is no GUI. How can I mix C++ and
    Python?

    One way is to run PY library as an independent process.
    Another way is to start PY as a C++ thread.
    The third way is to run Python engine as part of the C++.
    Which way is better?

    No intention to access all the Python's code but only
    access APIs of a module.
    Chang LI, Nov 25, 2004
    #1
    1. Advertising

  2. Chang LI

    Robert Kern Guest

    Chang LI wrote:
    > I want to use the Python Libray from my C++ program.
    > The Python Libray is no GUI. How can I mix C++ and
    > Python?
    >
    > One way is to run PY library as an independent process.
    > Another way is to start PY as a C++ thread.
    > The third way is to run Python engine as part of the C++.
    > Which way is better?
    >
    > No intention to access all the Python's code but only
    > access APIs of a module.


    Try Elmer.

    http://elmer.sf.net

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Nov 25, 2004
    #2
    1. Advertising

  3. Chang LI

    zhao Guest

    Chang LI wrote:
    > I want to use the Python Libray from my C++ program.
    > The Python Libray is no GUI. How can I mix C++ and
    > Python?
    >
    > One way is to run PY library as an independent process.
    > Another way is to start PY as a C++ thread.
    > The third way is to run Python engine as part of the C++.
    > Which way is better?
    >
    > No intention to access all the Python's code but only
    > access APIs of a module.


    Try boost.python and SWIG?
    (boost.sourceforge.net, swig.sourceforge.net)

    Both provide a interface between C/C++ and python
    if you want gui under python, you also can try wxpython or pygtk
    zhao, Nov 25, 2004
    #3
  4. Chang LI

    Chris S. Guest

    zhao wrote:

    > Try boost.python and SWIG?
    > (boost.sourceforge.net, swig.sourceforge.net)
    >
    > Both provide a interface between C/C++ and python
    > if you want gui under python, you also can try wxpython or pygtk


    I thought SWIG ported C/C++ libraries to Perl, Python, Ruby, Tcl, etc?
    How would you use it to port Python into C/C++?

    I think what you're looking for is instruction on how to embed Python in
    your C/C++ application.
    Chris S., Nov 25, 2004
    #4
  5. Chang LI a écrit :
    > I want to use the Python Libray from my C++ program.
    > The Python Libray is no GUI. How can I mix C++ and
    > Python?
    >
    > One way is to run PY library as an independent process.
    > Another way is to start PY as a C++ thread.
    > The third way is to run Python engine as part of the C++.
    > Which way is better?
    >
    > No intention to access all the Python's code but only
    > access APIs of a module.


    If you just want to access the Python API (or some APIs of modules) you
    need to launch the interpreter (in the current thread will be ok) and
    then you can access everything ... import modules, create objects, etc.

    Pierre
    Pierre Barbier de Reuille, Nov 25, 2004
    #5
  6. Chang LI

    Chang LI Guest

    Pierre Barbier de Reuille <> wrote in message news:<41a5b79a$0$17605$>...
    >
    > If you just want to access the Python API (or some APIs of modules) you
    > need to launch the interpreter (in the current thread will be ok) and
    > then you can access everything ... import modules, create objects, etc.
    >


    Yes. That is what I want (to launch Python Interpreter from C++ and use it)
    Where is the document?

    > Pierre
    Chang LI, Nov 25, 2004
    #6
  7. Chang LI

    Chang LI Guest

    "Chris S." <> wrote in message news:<ukgpd.12554$Vy.6850@trndny06>...
    > zhao wrote:
    >
    > > Try boost.python and SWIG?
    > > (boost.sourceforge.net, swig.sourceforge.net)
    > >
    > > Both provide a interface between C/C++ and python
    > > if you want gui under python, you also can try wxpython or pygtk

    >
    > I thought SWIG ported C/C++ libraries to Perl, Python, Ruby, Tcl, etc?
    > How would you use it to port Python into C/C++?
    >


    SWIG is for Python to use C++. I am searching for in reverse.

    > I think what you're looking for is instruction on how to embed Python in
    > your C/C++ application.
    Chang LI, Nov 25, 2004
    #7
  8. Chang LI

    Craig Ringer Guest

    On Fri, 2004-11-26 at 01:13, Chang LI wrote:

    > Yes. That is what I want (to launch Python Interpreter from C++ and use it)
    > Where is the document?


    http://docs.python.org/

    Check out the C API reference and the embedding tutorial.

    --
    Craig Ringer
    Craig Ringer, Nov 25, 2004
    #8
  9. Chang LI

    Robert Kern Guest

    Chang LI wrote:
    > "Chris S." <> wrote in message news:<ukgpd.12554$Vy.6850@trndny06>...
    >
    >>zhao wrote:
    >>
    >>
    >>>Try boost.python and SWIG?
    >>>(boost.sourceforge.net, swig.sourceforge.net)
    >>>
    >>>Both provide a interface between C/C++ and python
    >>>if you want gui under python, you also can try wxpython or pygtk

    >>
    >>I thought SWIG ported C/C++ libraries to Perl, Python, Ruby, Tcl, etc?
    >>How would you use it to port Python into C/C++?
    >>

    >
    >
    > SWIG is for Python to use C++. I am searching for in reverse.


    Elmer is the reverse of SWIG, and should be easier to use than using the
    Python C API itself. Elmer will handle the conversion of types between
    Python and C++ and will expose a nice API on the C++ side.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Nov 25, 2004
    #9
  10. Chang LI

    Chang LI Guest

    Robert Kern <> wrote in message news:<co3qur$qdl$>...
    >
    > Try Elmer.
    >
    > http://elmer.sf.net


    Looks like a perfect match between Python and C++.
    Great thanks!

    Chang
    Chang LI, Nov 25, 2004
    #10
  11. Chang LI

    zhao Guest

    swig is a interface of C/C++ to python, but not in reverse.

    In term of python document, there are extend and embed, and your need
    is embed.

    below is a short tutorial, maybe helpful:
    http://tutorials.impereonsoft.com/python/extemb.html

    if you work in windows, and you have install pywin32 package, it expose
    a com interface of python interpreter,
    so you also can use python's script or module in your application by
    this interface.
    if you use BCB, you can use a VCL component name "Python for Delphi".
    zhao, Nov 26, 2004
    #11
  12. Robert Kern <> wrote in message news:<co3qur$qdl$>...
    > Chang LI wrote:
    > > I want to use the Python Libray from my C++ program.
    > > The Python Libray is no GUI. How can I mix C++ and
    > > Python?

    .... (snip)
    >
    > Try Elmer.
    > http://elmer.sf.net


    There is also a library called PyCXX at http://cxx.sourceforge.net/.
    It's main purpose is to provide a convenient C++ interface to the
    Python C API. For example, it takes care of reference counting etc.
    Although it's purpose is to write C++ extensions for Python, it should
    be fairly easy to use it in reverse. It's a nice lib either way.
    Wynand Winterbach, Nov 26, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anders Eriksson

    I want to use a C++ library from Python

    Anders Eriksson, Sep 10, 2008, in forum: Python
    Replies:
    6
    Views:
    277
    Uwe Schmitt
    Sep 11, 2008
  2. Paul  Moore
    Replies:
    3
    Views:
    2,359
    Paul Moore
    May 11, 2009
  3. Michael Crute
    Replies:
    0
    Views:
    485
    Michael Crute
    Jun 14, 2010
  4. geremy condra
    Replies:
    0
    Views:
    395
    geremy condra
    Jun 14, 2010
  5. Simon Hardy-Francis
    Replies:
    2
    Views:
    45
    Rustom Mody
    Mar 23, 2014
Loading...

Share This Page