开发者

C ->Python Import Wrapper Problems

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开发者_StackOverflow社区 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜