Import / export values to/from C program

Rick Ratzel rick.ratzel at magma-da.com
Tue Apr 13 17:01:21 EDT 2004


Steffen Jahn wrote:
> Hi,
> 
> I stumbled across Python when trying to invoke *scripts* from C programs. The 
> idea is to set up some variables in the C program, export them, run a Python 
> script, and, after completion, import some variables back into the C program.
> This way, I can keep the C program very flexible for certain types of changes.
> 
> Unfortunately, I can't see an API to export/import an PyObject directly. 
> I see currently only an indirect way: Implement import and export functions
> in C which have to be called in the Python script (import function at 
> beginning and export function at end). Though I would prefer a *direct* access.
> 
> At the moment, I start thinking whether direct access is really better since
> the script needs anyways to *know* of the variables which are available (makes
> not much difference whether variable or function...)
> 
> Anyways, maybe somebody can tell me if such direct access is possible. A kick 
> in the right direction would be highly appreciated.
> 

    Not sure if this will help, but Elmer lets you run your Python code 
from C, where you can pass values back and forth between the languages 
without having to know the Python/C API.  You do have to define an Elmer 
interface file for your Python module though, but its pretty easy. 
There are some examples on the website:

http://elmer.sourceforge.net

    You did touch upon one of the main motivations for writing Elmer, 
and that is to keep the calling C/C++ program flexible for changes and 
prototyping new functionality...which is something Python is ideal for. 
  Since Elmer generates an interface that looks "native" to C, you can 
easily replace the underlying Python code with C/C++ once you are 
through prototyping...if you think it's necessary.



More information about the Python-list mailing list