此ImportError的含义是什么

此ImportError的含义是什么

本文介绍了导入Cython生成的.so文件时,此ImportError的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览Cython文档并构建每个示例应用程序。我对使用C库有些困惑。成功构建.so文件并尝试将其导入名为test.py的python文件后,将引发以下错误。

I am walking through the Cython documentation and building each of the example apps. I'm a little stuck at Using C Libraries. After successfully building the .so file and attempting to import it in a python file called test.py the following error is thrown.

$ python3.2 test.py
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    from queue import Queue
ImportError: dlopen(/Users/jeremy/Development/labs/python/cython_lib_wrapper/queue.so, 2): Symbol not found: _queue_free
  Referenced from: /Users/jeremy/Development/labs/python/cython_lib_wrapper/queue.so
  Expected in: flat namespace
 in /Users/jeremy/Development/labs/python/cython_lib_wrapper/queue.so

.so文件位于test.py文件的旁边。因此,似乎应该找到它。
它正在运行最新版本的Cython,在OSX 10.6上具有Python 3.2。

The .so file sits right next to the test.py file. So, it seems as though it should be found.This is running the latest version of Cython, with Python 3.2 on a OSX 10.6.

有什么见解吗?

编辑-添加构建命令和输出

$ python3.2 setup.py build_ext --inplace
running build_ext
cythoning queue.pyx to queue.c
building 'queue' extension
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -I/Library/Frameworks/Python.framework/Versions/3.2/include/python3.2m -c queue.c -o build/temp.macosx-10.6-intel-3.2/queue.o
    queue.c: In function ‘__pyx_f_5queue_5Queue_append’:
    queue.c:627: warning: cast to pointer from integer of different size
    queue.c: In function ‘__pyx_f_5queue_5Queue_extend’:
    queue.c:740: warning: cast to pointer from integer of different size
    queue.c: In function ‘__pyx_f_5queue_5Queue_peek’:
    queue.c:813: warning: cast from pointer to integer of different size
    queue.c: In function ‘__pyx_f_5queue_5Queue_pop’:
    queue.c:965: warning: cast from pointer to integer of different size
    gcc-4.2 -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -isysroot /Developer/SDKs/MacOSX10.6.sdk -g build/temp.macosx-10.6-intel-3.2/queue.o -o

编辑2-添加评论中要求的 otool cmd

queue.so:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)

编辑3-添加 nm输出

U ___stack_chk_fail
U ___stack_chk_guard
U _queue_free
U _queue_is_empty
U _queue_new
U _queue_peek_head
U _queue_pop_head
U _queue_push_tail
U dyld_stub_binder

grep cmd输出以下内容:

grep cmd outputs this:

(undefined) external _queue_free (dynamically looked up)


推荐答案

编辑:

啊,您没有提到您对libcalg中的代码有依赖性。在构建扩展名时,需要编译并包含这些内容。

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.

只需修改setup.py:

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:

我尝试了以下操作(3个文件,myext.pyx,test.py,setup.py ),它似乎可以正常工作。当然,我使用的是OS X 10.7,因此它与您的环境并不完全相同。要排除差异,也许您可​​以复制这些内容并将其构建为健全性检查。

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.

myext.pyx的内容:

Contents of myext.pyx:

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

test.py的内容

Contents of test.py

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

setup.py的内容:

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
)

我在包含以下3个文件的目录中构建:

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:

我的环境输出了类似的otool,并且DYLD_LIBRARY_PATH也未设置,但是nm -m显示定义的平方。

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

具体地说:

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

请试一下,看看nm -m在您的环境中显示什么。

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

这篇关于导入Cython生成的.so文件时,此ImportError的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:57