1

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.

gotgenes
  • 38,661
  • 28
  • 100
  • 128

1 Answers1

2

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}"
gotgenes
  • 38,661
  • 28
  • 100
  • 128