What is the meaning of this ImportError when importing a Cython generated .so file?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 04:23:16

EDIT:

Ah, you didn't mention you had a dependency on the code in libcalg. That stuff needs to be compiled and included when you build the cextension.

Just modify setup.py:

# setup.py
# ...
ext_modules = [Extension("queue", ["queue.pyx", "libcalg/queue.c"])]
# ...

We could step back and see if you can build a really simple example:

I've tried the following (3 files, myext.pyx, test.py, setup.py) and it appears to work fine. Of course I'm on OS X 10.7 so it's not exactly the same as your environment. To rule out differences perhaps you can copy these and build them as a sanity check.

Contents of myext.pyx:

# myext.pyx
def square(x):
    return x * x

Contents of test.py

# test.py
from myext import square
print "%d squared is %d"%(4, square(4))

Contents of setup.py:

# setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("myext", ["myext.pyx"])]

setup(
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

I build in the directory containing these 3 files:

cython_test$ /usr/bin/python setup.py build_ext --inplace 
running build_ext
cythoning myext.pyx to myext.c
building 'myext' extension
creating build
creating build/temp.macosx-10.7-intel-2.7
llvm-gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c myext.c -o build/temp.macosx-10.7-intel-2.7/myext.o
llvm-gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup -Wl,-F. -arch i386 -arch x86_64 build/temp.macosx-10.7-intel-2.7/myext.o -o /Users/steder/SO/cython_test/myext.so

cython_test$ python test.py
4 squared is 16:

My environment has similar otool output and DYLD_LIBRARY_PATH is also unset but nm -m shows squared as defined.

Specifically:

00000000000011d0 (__DATA,__data) non-external ___pyx_k__square
00000000000011e0 (__DATA,__data) non-external ___pyx_mdef_5myext_square
0000000000001218 (__DATA,__bss) non-external ___pyx_n_s__square
0000000000000c80 (__TEXT,__text) non-external ___pyx_pf_5myext_square

Please give this a shot and see what it nm -m shows in your environment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!