0

In an attempt to call a c function from Python, (in a previous post Calling a C function from a Python file. Getting error when using Setup.py file), I have compiled the code into a .pyd file and am testing the program. However, I am coming across the error

AttributeError: 'module' object has no attribute 'addTwo'

My test file is as so:

import callingPy
a = 3
b = 4
s = callingPy.addTwo(a, b)
print("S", s)

Where callingPy is the following .c file (turned into a .pyd) through compilation:

#include <Python.h>
#include "adder.h"

static PyObject* adder(PyObject *self, PyObject *args)       
{
    int a;
    int b;
    int s;
    if (!PyArg_ParseTuple(args,"ii",&a,&b))                      
       return NULL;
    s = addTwo(a,b);                                                
    return Py_BuildValue("i",s);                                
}

/* DECLARATION OF METHODS*/
static PyMethodDef ModMethods[] = {
    {"modsum", adder, METH_VARARGS, "Descirption"},         
    {NULL,NULL,0,NULL}
};

// Module Definition Structure
static struct PyModuleDef summodule = {
   PyModuleDef_HEAD_INIT,"modsum", NULL, -1, ModMethods     
};

/* INITIALIZATION FUNCTION*/
PyMODINIT_FUNC PyInit_callingPy(void)
{
    PyObject *m;
    m = PyModule_Create(&summodule);
    return m; 
}

Any help would be greatly appreciated! Thank you.

3
  • No where in the C code that you've posted do I see an addTwo function. Commented Dec 30, 2015 at 1:55
  • @David Hoelzer: The addTwo function is in another file, where the prototype is defined in the header function "Adder.h" Commented Dec 30, 2015 at 2:31
  • Your compiler error tends to disagree with you. Commented Dec 30, 2015 at 2:32

1 Answer 1

1

The only function in the extension module is exported to Python under the name modsum. You called addTwo. This seems self-explanatory.

It looks like at the C layer, there is a raw C function named addTwo that does the work for the C function adder, which is then exported to Python under the name modsum. So you should either rename the export, or call it with the correct name:

s = callingPy.modsum(a, b)

It looks like you copy-pasted a skeleton extension module, switched one tiny internal, and didn't fix up any of the exports or names.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the comment, although it is false that I copy-pasted a skeleton extension module. I did follow an example, but wrote it myself from that example so I knew what was going on. However, you are correct, that the import name had to be the function.c file that compiled my python code and the function name had to be my python method name. Thank you!
@smallfacebigmouth: You're welcome. I based the skeleton copy guess on the fact that the code is actually naming the module modsum too, even though the file produced is named callingPy.OSSPECIFICEXTENSION and imports as callingPy. It looks an awful lot like a module written for the purpose of modular summation that swapped in a simpler function later on. You see the same string scattered around all over, when the string isn't part of the intended naming or functionality, and you make assumptions. :-)
True. I think I'm still trying to get used to the concept of calling c from python :) thank you again for your help! I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.