By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,356 Members | 2,752 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,356 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Thanks for your time.

Aug 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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?

Thanks for your time.
This is actually a C question, not a Python question.

If all you need is the return value of the program, consider looking up
the system() call. For more control, variations on popen and fork/exec
may be fruitful on Unix (presumably OS X too), or CreateProcess* on
MS-Windows.

For more detailed help, try comp.unix.programmer or
comp.os.ms-windows.programmer (or a similar newsgroup for whatever OS
you're using).

Aug 16 '06 #2

P: n/a

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.

(c) none of the above

Cheers,
John

Aug 16 '06 #3

P: n/a

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

Aug 16 '06 #4

P: n/a

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

Aug 16 '06 #5

P: n/a

John Machin wrote:
>
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
Thanks, that'll help.

Aug 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.