Mahesh
Mahesh

Reputation: 13

Can't import C extension file in Python

My extension C function, in pyc.c

#include<Python.h>
static PyObject *add(PyObject* self,PyObject* args) {

    int a=9;
    int b=10;
    int c=a+b;
    return Py_BuildValue(c);

    //return Py_BuildValue( "s","hiiiii");
}

static char prin_docs[] = "prin( ): Any message you want to put here!!\n";

static PyMethodDef consume_methods[] = {
   {"add", (PyCFunction)add,
       METH_NOARGS, prin_docs},
       {NULL}
};

PyMODINIT_FUNC initadd(void) {
   Py_InitModule3(add, consume_methods,
                  "Extension module example!");
}

My setup file:

from distutils.core import setup, Extension
setup(name='add', version='1.0',  
      ext_modules=[Extension('add', ['pyc.c'])])

My Python program that uses that C file:

import add

print(add.add());

The above code successfully created a .so object but I can't import in the Python program by executing that. I get the following error:

Traceback (most recent call last):
  File "pycall.py", line 1, in <module>
    import add
ImportError: No module named add

Upvotes: 1

Views: 2224

Answers (1)

ivan_pozdeev
ivan_pozdeev

Reputation: 36018

Did you actually setup.py install your module? If you only built it, it won't be on sys.path.

Upvotes: 1

Related Questions