Connecting Tech Pros Worldwide Help | Site Map

Use Python library from C++

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2005, 06:04 PM
Chang LI
Guest
 
Posts: n/a
Default Use Python library from C++

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.

  #2  
Old July 18th, 2005, 06:04 PM
Robert Kern
Guest
 
Posts: n/a
Default Re: Use Python library from C++

Chang LI wrote:[color=blue]
> 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.[/color]

Try Elmer.

http://elmer.sf.net

--
Robert Kern
rkern@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
  #3  
Old July 18th, 2005, 06:04 PM
zhao
Guest
 
Posts: n/a
Default Re: Use Python library from C++


Chang LI wrote:[color=blue]
> 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.[/color]

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

  #4  
Old July 18th, 2005, 06:04 PM
Chris S.
Guest
 
Posts: n/a
Default Re: Use Python library from C++

zhao wrote:
[color=blue]
> 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[/color]

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.
  #5  
Old July 18th, 2005, 06:04 PM
Pierre Barbier de Reuille
Guest
 
Posts: n/a
Default Re: Use Python library from C++

Chang LI a écrit :[color=blue]
> 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.[/color]

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
  #6  
Old July 18th, 2005, 06:04 PM
Chang LI
Guest
 
Posts: n/a
Default Re: Use Python library from C++

Pierre Barbier de Reuille <pierre.barbier@cirad.fr> wrote in message news:<41a5b79a$0$17605$636a15ce@news.free.fr>...[color=blue]
>
> 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.
>[/color]

Yes. That is what I want (to launch Python Interpreter from C++ and use it)
Where is the document?
[color=blue]
> Pierre[/color]
  #7  
Old July 18th, 2005, 06:04 PM
Chang LI
Guest
 
Posts: n/a
Default Re: Use Python library from C++

"Chris S." <chrisks@NOSPAM.udel.edu> wrote in message news:<ukgpd.12554$Vy.6850@trndny06>...[color=blue]
> zhao wrote:
>[color=green]
> > 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[/color]
>
> I thought SWIG ported C/C++ libraries to Perl, Python, Ruby, Tcl, etc?
> How would you use it to port Python into C/C++?
>[/color]

SWIG is for Python to use C++. I am searching for in reverse.
[color=blue]
> I think what you're looking for is instruction on how to embed Python in
> your C/C++ application.[/color]
  #8  
Old July 18th, 2005, 06:04 PM
Craig Ringer
Guest
 
Posts: n/a
Default Re: Use Python library from C++

On Fri, 2004-11-26 at 01:13, Chang LI wrote:
[color=blue]
> Yes. That is what I want (to launch Python Interpreter from C++ and use it)
> Where is the document?[/color]

http://docs.python.org/

Check out the C API reference and the embedding tutorial.

--
Craig Ringer

  #9  
Old July 18th, 2005, 06:04 PM
Robert Kern
Guest
 
Posts: n/a
Default Re: Use Python library from C++

Chang LI wrote:[color=blue]
> "Chris S." <chrisks@NOSPAM.udel.edu> wrote in message news:<ukgpd.12554$Vy.6850@trndny06>...
>[color=green]
>>zhao wrote:
>>
>>[color=darkred]
>>>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[/color]
>>
>>I thought SWIG ported C/C++ libraries to Perl, Python, Ruby, Tcl, etc?
>>How would you use it to port Python into C/C++?
>>[/color]
>
>
> SWIG is for Python to use C++. I am searching for in reverse.[/color]

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
rkern@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
  #10  
Old July 18th, 2005, 06:05 PM
Chang LI
Guest
 
Posts: n/a
Default Re: Use Python library from C++

Robert Kern <rkern@ucsd.edu> wrote in message news:<co3qur$qdl$1@news1.ucsd.edu>...[color=blue]
>
> Try Elmer.
>
> http://elmer.sf.net[/color]

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

Chang
  #11  
Old July 18th, 2005, 06:05 PM
zhao
Guest
 
Posts: n/a
Default Re: Use Python library from C++

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".

  #12  
Old July 18th, 2005, 06:06 PM
Wynand Winterbach
Guest
 
Posts: n/a
Default Re: Use Python library from C++

Robert Kern <rkern@ucsd.edu> wrote in message news:<co3qur$qdl$1@news1.ucsd.edu>...[color=blue]
> Chang LI wrote:[color=green]
> > I want to use the Python Libray from my C++ program.
> > The Python Libray is no GUI. How can I mix C++ and
> > Python?[/color][/color]
.... (snip)[color=blue]
>
> Try Elmer.
> http://elmer.sf.net[/color]

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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.