Calling a python script, and getting the returned result in C

John Machin sjmachin at lexicon.net
Wed Aug 16 19:03:26 EDT 2006


Shuaib wrote:
> John Machin wrote:
> > Shuaib wrote:
> > > Hi!
> > >
> > > I have a python script which returns an Integer value. How do I call
> > > this script from a C programe, and use the result returned?
> >
> > To avoid confusion and possible irrelevant responses, please say which
> > of the following options best matches your requirement:
> >
> > (a) your Python script is capable of being run from the command line,
> > and "returns" an integer value by calling sys.exit(that_value) -- you
> > wish to execute the script from a C program [the same way you would
> > execute a shell script / awk script / ...] and pick up the return value
> > [which may be limited by the OS to range(0, 128)]
> >
> > (b) your script is a module, containing a function that returns an
> > integer. You wish to create an embedded Python interpreter, import
> > yourmodule, call yourmodule.yourfunc, convert the returned Python int
> > to a C int, and use it.
>
> (b) it is. :)

Then you need to read the Python manuals (surprise, surprise); in
particular here's a section that gives you most if not all of what you
want :

http://docs.python.org/ext/pure-embedding.html

but I'd suggest that you start reading a few pages back from there.

HTH,
John




More information about the Python-list mailing list