1

I have defined the name of my wrapper object in my c file blargUtils.c like this (I have defined methods and the lot for it in Blargmethods)...

void initBlarg(){
    Py_InitModule("Blarg", Blargmethods);
}

I compiled it like so...

blarglib: blargUtils.c
    gcc -I/usr/include/python2.6 -fPIC -c blargUtils.c -Wall    
    gcc -shared blargUtils.o -o blargUtils.so
clean:  
    rm *.so

However, when I try to import the wrapper in my python script...

import Blarg

Its says it says: "ImportError: No module named Blarg". I'm a little lost here and I don't understand why it cannot find the class when they are the exact same spelling. Maybe its a logic error?

If more code is needed let me know.

Chris Allen
  • 653
  • 4
  • 9
  • 17
  • I forgot to mention the .os file compiled if that helps anyway – Chris Allen Mar 16 '11 at 20:10
  • Where did you put the compiled .so file? (What directory?) Where are you trying to import it from? Like any other Python module, modules compiled in C need to be on the Python path to be found. – dappawit Mar 16 '11 at 20:17
  • 2
    Where are you putting your .so file? And what happens if you rename it to Blarg.so? – DNS Mar 16 '11 at 20:18
  • its all in the same directory .os, .c, .py and makefile – Chris Allen Mar 16 '11 at 20:21
  • im stupid in my comments, I meant .so instead of .os and to DNS, nothing happens when you alter the name. and run python again. I get the same problem. – Chris Allen Mar 16 '11 at 20:33

1 Answers1

1

First of all, from looking at the comments, I see that renaming it didn't work. This means (1) python can't find the .so file, (2) the .so file isn't usable (i.e. not compiled correctly or not all required symbols are found), or (3) there is a .py/.pyc/.pyo file in the same directory which already has that name. If you have Blarg.py already defined, python will look at this file first. The same goes if you have a directory named Blarg in your search path. So instead of bashing your head against the wall, try this:

1) Rename your .so library to something guaranteed not to collide (i.e. _Blarg)

void initBlarg() {
    Py_InitModule("_Blarg", Blargmethods);
}

2) Compile it with the SAME NAME

gcc -I/usr/include/python2.6 -fPIC -c blargUtils.c -Wall    
gcc -shared blargUtils.o -Wl,-soname -Wl,_Blarg.so -o _Blarg.so

3) Create a python wrapper (i.e. Blarg.py)

import sys
sys.path.append('/path/to/your/library')

import _Blarg

def blargFunc1(*args):
    """Wrap blargFunc1"""
    return _Blarg.blargFunc1(*args)

4) Now just use it as normal

import Blarg
Blarg.blargFunc1(1, 2, 3)

Obviously this is a bit of overkill, but it should help you determine where the problem is. Hope this helps.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119