473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
5 2536
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

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
5
by: Ralph Sluiters | last post by:
Hi, i've got a small problem with my python-script. It is a cgi-script, which is called regulary (e.g. every 5 minutes) and returns a xml-data-structure. This script calls a very slow function,...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
1
by: robert.differentone | last post by:
Could anybody tell me how to call python modules from C. e.g. my Python module : def add(a,b) return(a+b) How can I call "add.py" from C? Please post any simple example. I have read Python...
3
by: Zeez | last post by:
Hi all, I have a webpage that calls a webservice found on a remote computer. Below is the code of my webpage: ===================================== <html> <head> <script...
2
by: Shuaib | last post by:
Hey! How do I call a python script from a C programme, and get results from the python script (which is just a single integer value)? Thanks.
1
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python...
4
by: Alchemist | last post by:
I am using Python 2.4 and Postgresql 8.2 database server. On the database I have created a stored function, example, CREATE OR REPLACE FUNCTION calculateaverage() I created a new python script...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.