473,757 Members | 6,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling python functions from C

I am a newbie to Python. I want to call python functions from C. I
looked for examples but I couldn't get any simple one. Lets say my
python code is :
def add(a,b)
return (a+b)

I want to call add from C. Could anybody please help me? Thanks in
advance.

R.

May 10 '06 #1
5 2079
ro************* ****@gmail.com wrote:
I want to call add from C. Could anybody please help me?


Look at the "Pure Embedding" section of the python documentation. It
contains an example program that does exactly what you want. Here is a
link to the page:

http://www.python.org/doc/ext/pure-embedding.html

-Farshid
May 10 '06 #2
On 9 May 2006 10:05:48 -0700, ro************* ****@gmail.com wrote:
I am a newbie to Python. I want to call python functions from C. I
looked for examples but I couldn't get any simple one. Lets say my
python code is :
def add(a,b)
return (a+b)

I want to call add from C. Could anybody please help me? Thanks in
advance. This is also known as "callback".

Check out
http://www.google.com/search?client=...utf-8&oe=utf-8
or
http://www.python.org/doc/current/ext/ext.html

-RM
R.

May 10 '06 #3
Thanks Farshid,

Thats helps me alot !

R.
Farshid Lashkari wrote:
ro************* ****@gmail.com wrote:
I want to call add from C. Could anybody please help me?


Look at the "Pure Embedding" section of the python documentation. It
contains an example program that does exactly what you want. Here is a
link to the page:

http://www.python.org/doc/ext/pure-embedding.html

-Farshid


May 10 '06 #4
Thanks Ronny,

R.

Ronny Mandal wrote:
On 9 May 2006 10:05:48 -0700, ro************* ****@gmail.com wrote:
I am a newbie to Python. I want to call python functions from C. I
looked for examples but I couldn't get any simple one. Lets say my
python code is :
def add(a,b)
return (a+b)

I want to call add from C. Could anybody please help me? Thanks in
advance.

This is also known as "callback".

Check out
http://www.google.com/search?client=...utf-8&oe=utf-8
or
http://www.python.org/doc/current/ext/ext.html

-RM

R.


May 10 '06 #5
ro************* ****@gmail.com wrote:
I am a newbie to Python. I want to call python functions from C. I
looked for examples but I couldn't get any simple one. Lets say my
python code is :
def add(a,b)
return (a+b)

I want to call add from C. Could anybody please help me? Thanks in
advance.

R.


You might look at elmer <http://elmer.sourcefor ge.net/>

Quote:
What is elmer?
Elmer is a tool which embeds a Python module into a C, C++, or Tcl
application. The embedded Python module is used just as if it was
written in the same language as the application itself, without
requiring any knowledge of the Python/C API.

Elmer generates "glue" code that embeds an interpreter for one type of
language into an extension for another. Elmer allows function calls and
various data types, both native and custom, to pass transparently
between the two languages.
--
Nigel Rowe
A pox upon the spammers that make me write my address like..
rho (snail) swiftdsl (stop) com (stop) au
May 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2043
by: Kyler Laird | last post by:
I need to submit C/C++ code for a class. (It's not a programming class. The choice of language is inertial. I think that it mostly serves to distract students from the course subject.) I'm fairly fluent with C but it hurts to think about writing in C when Python is *so* much more appropriate for these operations. I'd like to keep my sanity and satisfy the course requirements by programming in Python and converting the code to C. It...
0
2306
by: Natsu Mizutani | last post by:
Hello, I'm trying to wrap a c++ library using MPI inside with boost.python (or SWIG). I managed to find that calling `MPI::Init()` embeded in a c++ funtion would not work. So, I decided to use `pyMPI`. To avoid overhead of pickling and unpickling while calling `mpi.send` in Python, I'd rather call c++ functions `MPI::Isend()` etc. embeded in a c++ function.
18
5301
by: bart_nessux | last post by:
I need a script to call several functions at the same time. How does one call more than one function simultaneously?
1
1738
by: delrocco | last post by:
Hello, I really appreciate anyone who has time to read this and help, Thanks up front. I'm very new to python, having picked it up for the first time a week ago, but I feel I'm very close to getting this working. Here's what I'm trying to do: - call python scripts from game code - have those called scripts call functions that are back in game code
6
1547
by: Yevgeniy (Eugene) Medynskiy | last post by:
Hi all, This is probably a very newbie question, but after searching google and docs @ python.org I can't find an answer, so maybe someone would be able to help? I'd like to call command-line functions from my python script (like you would in perl using backticks)... Is there a way of doing this? And if so, how does the environment get treated (I have some variables in my env that the programs I'd be calling need to see).
1
1923
by: Gerald Klix | last post by:
I read the whol email thread carefully and could not find any sentence by Guido, which states that he does not accept ctypes for the standard library. He just declined to rewrite winreg. Did I miss something? Cya, Gerald -----Ursprüngliche Nachricht----- Von: python-list-bounces+gerald.klix=klix.ch@python.org Im Auftrag von
3
5467
by: David Bear | last post by:
I have a hash function written by another organization that I need to use. It is implemented in perl. I've been attempting to decode what they are doing in their hash function and it is taking way too long. I've identified two functions in a perl module that I would like to 'call' from a python program. I found the following: http://www.annocpan.org/~GAAS/pyperl-1.0/perlmodule.pod and wondered if anyone had any comments. This thing...
4
4801
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread completes it invokes a method from the calling thread. Sort event driven concept with threads. Thanks. Ed Gomez
47
4980
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
16
3785
by: Jaco Naude | last post by:
Hi there, This is my first post over here and I hope someone can give me some guidance. I'm trying to embed Python into a Visual C++ 2008 application and I'm getting linker problems. I've compiled a DLL of the Python source code using the pythoncode VC++ project in the PCbuild folder of the source download and this works 100% without any warnings etc. I've done this in Debug and Release mode without any problems.
0
9489
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
9298
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
10072
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
8737
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
6562
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();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.