Python ctypes 未在 Mac OS X 上加载动态库

发布于 2024-09-14 03:57:38 字数 695 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-09-21 03:57:38

这不仅仅是 dylib 中可用的架构问题;这也与 Python 解释器运行的体系结构有关。如果您在 OS X 10.6 中使用 Apple 提供的 Python 2.6.1,则默认情况下它会在可能的情况下以 64 位模式运行。既然你说你的库是编译为 32 位的,那么你需要强制 Python 在 32 位模式下运行。对于 Apple 提供的 Python,一种方法是设置一个特殊的环境变量:

$ python -c "import sys; print sys.maxint"
9223372036854775807
$ export VERSIONER_PYTHON_PREFER_32_BIT=yes
$ python -c "import sys; print sys.maxint"
2147483647

请参阅 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:

$ python -c "import sys; print sys.maxint"
9223372036854775807
$ export VERSIONER_PYTHON_PREFER_32_BIT=yes
$ python -c "import sys; print sys.maxint"
2147483647

See Apple's man 1 python for more information.

吻安 2024-09-21 03:57:38

没有。正如错误消息所示,您的 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. Use file to check what the architecture of librepeater.dylib is; your python is going to be built using one of the ones not listed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文