Re: Embedding python

John Buchanan (juancho@cs.ubc.ca)
27 Mar 92 9:58 -0800

>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.

I took a quick look at pythonmain.c and I have a couple of questions and or
suggestions.

main() seems simple enough, so I would like to suggest the following.

rewrite and rename main() as follows

initialize_python(int argc,char argv[])

write a separate module for python of which looks like this

the_real_python_main.c

main(int argc, char *argv[])
{
initialize_python(argc,argv);
}

Now compile everything but the_real_python_main.c as a library.
compile and link python.

Now to call from a c program we do the following

initialize_python(5 ,argv);

Now I realize that this is a simple approach and is probably not going
to work as stated but I think that the idea of making the bulk of python
into a library and then having a simple front end for it is the way to go.
This allows python to be embedded quite easily and would not result in
incompatible versions of python running around.

juancho