Mailing List Archive

Using C to execute Python
I have a C program and I want to call certain Python methods / functions
passing quite a few parameters.
How can I do this, in general fashion?
I would have to include python.h, I reckon - where can I find this, I've
been searching all over for it (is it just me???)
I'm no expert, far from, in Python...

Thomas
email: thomar@ifi.ntnu.no
Using C to execute Python [ In reply to ]
Hi!

On Wed, 23 Jun 1999, Thomas Røberg wrote:
> I have a C program and I want to call certain Python methods / functions
> passing quite a few parameters.
> How can I do this, in general fashion?

You need to embed python interpreter into your program. That is: you
link your program with libpython.a and call some functions (initilize, evel
string, etc.)
More details in the standard docs on Extending and Embedding Python.

> I would have to include python.h, I reckon - where can I find this, I've
> been searching all over for it (is it just me???)

It is in the source distribution.

> I'm no expert, far from, in Python...
>
> Thomas
> email: thomar@ifi.ntnu.no

Oleg.
----
Oleg Broytmann Netskate/Inter.Net.Ru phd@emerald.netskate.ru
Programmers don't die, they just GOSUB without RETURN.
Using C to execute Python [ In reply to ]
Thomas Røberg wrote:
>
> I have a C program and I want to call certain Python methods / functions
> passing quite a few parameters.
> How can I do this, in general fashion?
> I would have to include python.h, I reckon - where can I find this, I've
> been searching all over for it (is it just me???)
> I'm no expert, far from, in Python...

Have you read the Python/C Api? you can found it here:
http://www.python.org/doc/current/api/api.html


if you want to extend python using C have a look at:
http://www.python.org/doc/current/ext/ext.html

if you don't know where to start www.python.org is quite first choice.

Regards
Friedrich
Using C to execute Python [ In reply to ]
In comp.lang.python Thomas R.berg <thomar@ifi.ntnu.no> wrote:
: I have a C program and I want to call certain Python methods / functions
: passing quite a few parameters.
: How can I do this, in general fashion?
: I would have to include python.h, I reckon - where can I find this, I've
: been searching all over for it (is it just me???)
: I'm no expert, far from, in Python...

: Thomas
: email: thomar@ifi.ntnu.no

Hi Thomas,

You will need the full source distribution of Python (the default Linux
distribution, the binary distributions come with the header files, etc.).

Compile and install that. Then read "Extending and Embedding the
Python Interpreter" (http://www.python.org/doc/current/ext/ext.html)
and "Python/C API Reference Manual"
(http://www.python.org/doc/current/api/api.html) to describe how to
embed Python.

Very simply,
1) Initialize Python (with Py_Initialize())
2) Run some of the Python API calls (high-level or low-level)
3) When you are done, call Py_Finalize()

int callpython(python_cmd)
char *python_cmd;
{ int rc;

if (!Py_IsInitialized())
Py_Initialize();
rc = PyRun_SimpleString(python_cmd);
return rc;
}

The problem with this is that exceptions are not captured and are
printed to stderr directly. The lower-level API calls give finer
control, but luckily very easy to code (compared to other P*
languages).

-Arcege