Calling python functions from C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robert.differentone@gmail.com

    #1

    Calling python functions from C

    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 "ImportErro r"
    all the time?

    Any help would be appreciated !

    R.

  • John Machin

    #2
    Re: Calling python functions from C

    On 17/07/2006 6:47 AM, robert.differen tone@gmail.com wrote:
    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\in clude
    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\li bs
    -Lc:\python24\PC Build -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 "ImportErro r"
    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 "ImportErro r" without any following text, something
    is gravely wrong.

    If you are getting "ImportErro r" *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

    Comment

    Working...