473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
Jul 18 '05 #1
11 6626
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #2

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

Jul 18 '05 #3
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.
Jul 18 '05 #4
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
Jul 18 '05 #5
Pierre Barbier de Reuille <pi************@cirad.fr> wrote in message news:<41***********************@news.free.fr>...

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

Jul 18 '05 #6
"Chris S." <ch*****@NOSPAM.udel.edu> 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.

Jul 18 '05 #7
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

Jul 18 '05 #8
Chang LI wrote:
"Chris S." <ch*****@NOSPAM.udel.edu> 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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #9
Robert Kern <rk***@ucsd.edu> wrote in message news:<co**********@news1.ucsd.edu>...

Try Elmer.

http://elmer.sf.net


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

Chang
Jul 18 '05 #10
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".

Jul 18 '05 #11
Robert Kern <rk***@ucsd.edu> wrote in message news:<co**********@news1.ucsd.edu>...
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.
Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Stephen Ferg | last post by:
I am a very satisfied user of Python and have been for number of years. I would never willing use another language. I wish all good things for Python, and that moves me to express some thoughts...
217
by: gyromagnetic | last post by:
The following url points to an article written by Damian Conway entitled "Ten Essential Development Practices": http://www.perl.com/pub/a/2005/07/14/bestpractices.html Althought the article has...
20
by: Guy Fawkes | last post by:
Hi, I was wondering if Python programs always need to include the source code with the program itself. I'm asking this because I don't want my program to be open-source and so far all the Python...
1
by: Justin Johnson | last post by:
Hello, I'm trying to build Python 2.5.0 on AIX 5.3 using IBM's compiler (VisualAge C++ Professional / C for AIX Compiler, Version 6). I run configure and make, but makes fails with undefined...
4
by: Arnaud Delobelle | last post by:
Hi fellow python enthusiasts. Having recently acquired a MacBook Pro (Intel Core 2 Duo) which comes with python2.5, I have been installing some modules that I need (PIL, psycopg2, PyXML ...). ...
1
by: Con | last post by:
Hi, I'm gettting a traceback when I attempt to read the MySQLdb docs after installation on Mac OS X 10.5.2: BEGIN TRACEBACK: Traceback (most recent call last): File "<stdin>", line 1, in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.