Re: Embedding python

Guido van Rossum (Guido.van.Rossum@cwi.nl)
Fri, 27 Mar 1992 14:26:41 +0100

Embedding Python in C is possible, but misc/EXTENDING isn't the whole
answer. It only describes how to extend Python with code written in
C, but you still have to have a "main program" written in Python.

By definition, "embedding" means doing it the other way around, like
John writes:

>What I was looking for was something that looks as follows.
>
>int load_python_program(char *pathname_to_python_program)
> /* Or some function to initialize and load python */
>
>int run_python_program_with_arguments(arg1 arg2 arg3.....argn)
> /* call some procedure in the loaded program */

Such a simple interface currently does not exist, but it shouldn't be
too hard to create it, or something similar.

I hope you're a bit adventurous; then you can read pythonmain.c and
figure out how to do it. Basically, you'll need to move all functions
from that file elsewhere, and add calls to initpython() (at least) and
setpythonpath() (probably) and setpythonargv() (perhaps) to your own
main program. You can then call routines like run_command(string) or
run(FILEpointer, filename) to execute Python code as you wish. There
are other run_*() functions that provide more flexibility, like
choosing which environment to use.

You'll probably find that the Python code run in this way needs access
to funactionality defined in your application, and for *that* you'll
need to do everything described in misc/EXTENDING.

--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"What's so unpleasant about being drunk?"
"Go ask a glass of water!"