Calling python functions from C

  • Thread starter robert.differentone
  • Start date
R

robert.differentone

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?

Any help would be appreciated !

R.
 
J

John Machin

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've never contemplated embedding before, but have done some extending.
Your post made me curious. Here are my expedition notes:

1. Simple example (more elaborate than what you want to do) found in the
documentation: section 5.3 of the Extending and Embedding Manual. [about
1 minute]

2. Mucked about getting an executable created. [Windows, tried bcc32
first, gave up, got mingw's gcc to do it, total about 30 minutes]

This worked:

C:\devel\embed>gcc -mno-cygwin -O -Wall -Ic:\python24\include
Ic:\python24\PC -c runfunc.c -o runfunc.o
runfunc.c: In function `main':
runfunc.c:6: warning: unused variable `pDict'

C:\devel\embed>gcc -mno-cygwin -s runfunc.o -Lc:\python24\libs
-Lc:\python24\PCBuild -lpython24 -lmsvcr71 -o runfunc.exe

3. Created a test file [about 1 minute]

C:\devel\embed>type arith.py
def plus(a, b):
return a + b

def minus(a, b):
return a - b

def times(a, b):
return a * b

4. Some tests [about two minutes]

C:\devel\embed>runfunc
Usage: call pythonfile funcname [args]

C:\devel\embed>runfunc arith add 1 2
AttributeError: 'module' object has no attribute 'add'
Cannot find function "add"

C:\devel\embed>runfunc arith plus 1 2
Result of call: 3

C:\devel\embed>runfunc arith minus 1 2
Result of call: -1

C:\devel\embed>runfunc arith times 1 2
Result of call: 2

C:\devel\embed>runfunc arith times 6 7
Result of call: 42
I have read Python embedding in Python Manual but I get "ImportError"
all the time?

Sorry, my parser throws a syntax error on that; is it meant to be a
question or a statement?

If you are getting "ImportError" without any following text, something
is gravely wrong.

If you are getting "ImportError" *with* following text but not divulging
that text, you are implicitly asking your audience to play guessing
games. Your audience may have better ways of amusing themselves. At
best, after one or two desultory attempts they would probably give up.

Here's my first attempt:

C:\devel\embed>runfunc arith.py times 6 7
ImportError: No module named py
Failed to load "arith.py"

Well, am I hot/warm/cold?
Any help would be appreciated !

Self-help is often fastest to arrive.

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,249
Latest member
reactnativeexpert

Latest Threads

Top