[rp] [newbie] embedding python

Jim jbublitzNO at SPAMnwinternet.com
Sun Mar 24 04:11:05 EST 2002


Trung Hoang wrote:
> Hi, so urgent!

> i have a python script "file.py" which has a function in it which i need to
> call from my c code. how can i do this! all my projects have been postponded
> becuase i cant do this! please helP! i would be so grateful!!

> ultimately im trying to embed some python into my c source in UNIX.
> i have absoutely no idea how to do this despite the tutorial on the web at
> http://www.python.org/doc/current/ext/building-on-unix.html#building-on-unix

> it's too general and not step by step enough for a noob like me :(

{Responding here to newsgroup msg and private email]

The most detailed step-by-step explanation is in Mark Lutz'
'Programming Python' book. Another good place to look is in
the 'Python/C API' and "Extending/Embedding' docs that come
with Python, along with the example in the 'Demos/embedding'
directory that comes with the source distribution. I
personally am not that knowledgeable about embedding Python,
and in fact use C++ bindings (via sip) to avoid all of the
argument passing/return value issues.

My suggestion though would be that for a single function in
a single application, embedding is probably not the way to
go. Instead, I would look at using files, pipes or sockets
for passing data between C and Python, and execute the
Python script via a C 'exec' or 'system' call. It's perhaps
not as elegant as embedding Python, but it's more efficient
use of your time and probably uses things you already know
how to do.

Sorry I can't be of more help.

Jim








More information about the Python-list mailing list