ctypes doesn't find fink-installed libraries in /sw/lib

那年仲夏 提交于 2019-12-24 07:58:51

问题


ctypes isn't finding libraries installed via fink, which live under /sw/lib/, unless I explicitly give the full path to the libraries

>>> 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>
>>>

Compilation against these libraries with gcc, however, works fine; they are always found. Why isn't ctypes locating these libraries, and what can I do to make it locate them?

This is on OS X 10.6.8 with fink-installed Python 2.7 under /sw/bin/python2.7.


回答1:


The problem seems to be that fink never sets the LD_LIBRARY_PATH variable. ctypes uses dlopen() which will not search in /sw/lib by default. From the dlopen man page:

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.

So the solution seems to be to put in your .profile, .bash_profile, or .bashrc

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

It also seems that fink installs some libraries in subdirectories under /sw/lib, such as /sw/lib/mysql. In these cases, you will have to explicitly include those, as well, because it seems like the dlopen() does not recursively search the paths in LD_LIBRARY_PATH. In the case of MySQL, you would need to add that in the path:

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


来源:https://stackoverflow.com/questions/9775425/ctypes-doesnt-find-fink-installed-libraries-in-sw-lib

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