Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python runtime_library_dirs doesn't work on Mac

I have a Python extension module that needs to link against some dynamic libraries at runtime, so I need to tell it where to look for them. I'm doing this by specifying runtime_library_dirs in my setup.py. This works fine on Linux, but seems to have no effect on Mac. I get an ImportError when I try to import my module, and the only way I've found to make it go away is to add the library directory to DYLD_LIBRARY_PATH before starting python. What do I need to do to make this work?

like image 887
peastman Avatar asked Oct 01 '13 18:10

peastman


2 Answers

I finally figured this out. The solution has two parts. First, setup.py needs to use extra_link_args to tell the linker to add a correct rpath to the compiled module:

if platform.system() == 'Darwin':
    extra_link_args.append('-Wl,-rpath,'+lib_path)

where lib_path is the directory where the libraries are installed. Second, all of the libraries you're linking against must have install names that begin with "@rpath/". For example, if a library is called "libFoo.dylib", its install name should be "@rpath/libFoo.dylib". You can use "install_name_tool -id" to change the install name of a library.

like image 84
peastman Avatar answered Oct 14 '22 02:10

peastman


You can tell what libraries an extension links against with

otool -L pyext.so

I had a problem where an extension was linking to the wrong version of a library on my system. In that case I used install_name_tool to change the path to the library directly. For example,

install_name_tool -change /wrong/libfoo.dylib /right/libfoo.dylib pyext.so
like image 38
Brian Hawkins Avatar answered Oct 14 '22 02:10

Brian Hawkins