1

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
DavidW
  • 29,336
  • 6
  • 55
  • 86
Mahesh
  • 13
  • 5
  • Welcome to SO! Have you already read the [Cython tutorial](https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html) on this topic? It looks to me like you're missing a call to `cythonize()`. – Niayesh Isky Mar 18 '19 at 05:56
  • @NiayeshIsky I believe the "cpython" tag that you edited out was correct - thee user is using the "cpython" implementation of Python. This doesn't look to have anything to do with Cython so I removed that tag which you added. – DavidW Mar 18 '19 at 10:13
  • @DavidW Fair enough, though removing `cpython` in the first place was actually [OP's change](https://stackoverflow.com/posts/55215233/revisions). – Niayesh Isky Mar 18 '19 at 12:27

1 Answers1

1

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

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152