I have an import to import a shared object library created in c. It looks like this:
import Cal
I then try to make a call in python with the shared object library like so:
status = Cal.Cal_readFile(filename, result)
However when I hit this code when running my python file I get the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1541, in __call__
return self.func(*args)
File "xcal.py", line 72, in openDialog
status = Cal.Cal_readFile(filename, result)
AttributeError: 'module' object has no attribute 'Cal_readFile'
My makefile regarding the shared object library looks like this:
CC = gcc
CFLAGS = -Wall -std=c11 -g -DNDEBUG `pkg-config --cflags python3`
CFLAGS += -fPIC
LDFLAGS=`pkg-config --libs python3`
all: caltool Cal.so
Cal.so: calmodule.o calutil.o
$(CC) -shared $(LDFLAGS) $^ -o $@
calutil.o: calutil.c calutil.h
calmodule.o: calmodule.c calutil.c
And my C code looks like the following:
#include <python3.4/Python.h>
#include "calutil.h"
PyObject *Cal_readFile( PyObject *self, PyObject *args );
static PyMethodDef CalMethods[] = {
{"readFile", Cal_readFile, METH_VARARGS},
{NULL, NULL}
};
static struct PyModuleDef calModuleDef = {
PyModuleDef_HEAD_INIT,
"Cal", //enable "import Cal"
NULL, //omit module documentation
-1, //don't reinitialize the module
CalMethods //link module name "Cal" to methods table
};
PyMODINIT_FUNC PyInit_Cal(void) {
return PyModule_Create( &calModuleDef );
}
PyObject *Cal_writeFile( PyObject *self, PyObject *args ) {
return NULL;
}
Note: The Cal_writeFile function does actually have stuff happening in it, just I didn't want to paste the big function as it likely isn't needed to solve this.
Is there something I am doing wrong here? I feel like that Python import Cal should be properly be importing the shared object library named Cal.so that is in the same directory as the Python file.