473,659 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2565
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.progr ammer or
comp.os.ms-windows.program mer (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_v alue) -- 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.your func, 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_v alue) -- 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.your func, 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_v alue) -- 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.your func, 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
6545
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 FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
5
2079
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, with a duration of 10-40 seconds. To avoid delays, i inserted a cache for the data. So, if the script is called, it returns the last caculated data-structure and then the function is called again and the new data is stored in the cache. (There is no...
2
6907
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 a type mismatch. It is positively because of the boolean(java primitive)parameter. It goes fine if I change this parameter to int or String. This inteface has a lot more methods which works fine, it is just the
1
323
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 embedding in Python Manual but I get "ImportError" all the time?
3
2336
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 language="JavaScript"> var iCallID;
2
1326
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
2209
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 embedding it). I have a function that executes a string buffer of python code, fetches a function from its global dictionary then calls it. When the function code returns a local variable, PyObject_Call() appears to be returning garbage. ...
4
5673
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 and would like to call my database stored function. How can I call a database stored function/procedure in python?
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.