Python ctypes 未在 Mac OS X 上加载动态库
我有一个 C++ 库 repeater.so
,我可以通过以下方式从 Linux 中的 Python 加载该库:
import numpy as np
repeater = np.ctypeslib.load_library('librepeater.so', '.')
但是,当我在 Mac OS X(Snow Leopard,32 位)上编译相同的库并得到 repeater.dylib
,然后在 Python 中运行以下命令:
import numpy as np
repeater = np.ctypeslib.load_library('librepeater.dylib', '.')
我收到以下错误:
OSError: dlopen(/mydir/librepeater.dylib, 6): no suitable image found. Did find:
/mydir/librepeater.dylib: mach-o, but wrong architecture
我是否必须做一些不同的事情才能在 Mac OS X 上的 Python 中加载动态库?
I have a C++ library repeater.so
that I can load from Python in Linux the following way:
import numpy as np
repeater = np.ctypeslib.load_library('librepeater.so', '.')
However, when I compile the same library on Mac OS X (Snow Leopard, 32 bit) and get repeater.dylib
, and then run the following in Python:
import numpy as np
repeater = np.ctypeslib.load_library('librepeater.dylib', '.')
I get the following error:
OSError: dlopen(/mydir/librepeater.dylib, 6): no suitable image found. Did find:
/mydir/librepeater.dylib: mach-o, but wrong architecture
Do I have to do something different to load a dynamic library in Python on Mac OS X?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不仅仅是 dylib 中可用的架构问题;这也与 Python 解释器运行的体系结构有关。如果您在 OS X 10.6 中使用 Apple 提供的 Python 2.6.1,则默认情况下它会在可能的情况下以 64 位模式运行。既然你说你的库是编译为 32 位的,那么你需要强制 Python 在 32 位模式下运行。对于 Apple 提供的 Python,一种方法是设置一个特殊的环境变量:
请参阅 Apple 的
man 1 python
了解更多信息。It's not just a question of what architectures are available in the dylib; it's also a matter of which architecture the Python interpreter is running in. If you are using the Apple-supplied Python 2.6.1 in OS X 10.6, by default it runs in 64-bit mode if possible. Since you say your library was compiled as 32-bit, you'll need to force Python to run in 32-bit mode. For the Apple-supplied Python, one way to do that is to set a special environment variable:
See Apple's
man 1 python
for more information.没有。正如错误消息所示,您的 python 和 librepeater.dylib 文件之间存在架构不匹配。使用
file
检查librepeater.dylib
的架构是什么;您的 python 将使用未列出的其中之一来构建。Nope. As the error message says, there's an architecture mismatch between your python and
librepeater.dylib
file. Usefile
to check what the architecture oflibrepeater.dylib
is; your python is going to be built using one of the ones not listed.