gpt4 book ai didi

python - ctypes 在/sw/lib 中找不到 fink 安装的库

转载 作者:行者123 更新时间:2023-12-01 06:02:15 25 4
gpt4 key购买 nike

ctypes 找不到通过 fink 安装的库,这些库位于 /sw/lib/ 下,除非我明确给出库的完整路径

>>> import ctypes
>>> ctypes.CDLL('libgoffice-0.8.dylib')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/sw/lib/python2.7/ctypes/__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
OSError: dlopen(libgoffice-0.8.dylib, 6): image not found
>>> ctypes.CDLL('/sw/lib/libgoffice-0.8.dylib')
<CDLL '/sw/lib/libgoffice-0.8.dylib', handle 336500 at 2b10b0>
>>>

但是,使用 gcc 对这些库进行编译工作正常;他们总是被发现。为什么 ctypes 没有找到这些库,我该怎么做才能让它找到它们?

这是在 OS X 10.6.8 上,在 /sw/bin/python2.7 下安装了 fink 安装的 Python 2.7。

最佳答案

问题似乎是 fink 从不设置 LD_LIBRARY_PATH 变量。 ctypes 使用 dlopen() 默认情况下不会在 /sw/lib 中搜索。从 dlopen 手册页:

dlopen() searches for a compatible Mach-O file in the directories speci- fied by a set of environment variables and the process's current working directory. When set, the environment variables must contain a colon-sep- arated list of directory paths, which can be absolute or relative to the current working directory. The environment variables are LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, and DYLD_FALLBACK_LIBRARY_PATH. The first two vari- ables have no default value. The default value of DYLD_FALL- BACK_LIBRARY_PATH is $HOME/lib;/usr/local/lib;/usr/lib. dlopen() searches the directories specified in the environment variables in the order they are listed.

所以解决方案似乎是放入您的 .profile.bash_profile.bashrc

export LD_LIBRARY_PATH=/sw/lib:"${LD_LIBRARY_PATH}"

似乎 fink 在 /sw/lib 下的子目录中安装了一些库,例如 /sw/lib/mysql。在这些情况下,您还必须显式包含这些内容,因为 dlopen() 似乎不会递归搜索 LD_LIBRARY_PATH 中的路径。对于 MySQL,您需要将其添加到路径中:

export LD_LIBRARY_PATH=/sw/lib:/sw/lib/mysql:"${LD_LIBRARY_PATH}"

关于python - ctypes 在/sw/lib 中找不到 fink 安装的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9775425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com