pythonmacosdylib

How to make Python load a dylib on OSX?


I'm trying to load a shared lib out of the current '.' dir in a unit test on OSX.

What works on Linux and NetBSD is a symlink _mymodule.so --> ../.libs/libmymodule.so

But on OSX, Python's import mymodule won't find

_mymodule.dylib --> ../.libs/libmymodule.dylib

I've tried adding

export DYLD_LIBRARY_PATH=.:$DYLD_LIBRARY_PATH

to the script env, no go. Any help appreciated.

-Edit

uUdate 4/6/10:

Solved with the info from krunk below. But just copying or ln -s'ing the dylib to a .so name didn't solve it completely. Still wouldn't load. But telling libtool to link the lib with the -module flag created a .so lib that would load. Python version of the lib works now.

Now if I could just get the perl lib working. I'm building Swig Perl, Python, Ruby, and Lua libs and this fix only got Python and Lua working.


Solution

  • Just use *.so as your module extensions in OS X too. I have a vague memory of not being able to load .dylib's and it turning out to be an issue with python itself. . . but I can't find the mailing list post now.

    However, rest assured you're following standard practice by using *.so's even on OS X. The only *.dylib's in the entire framework are the libsvn_swig ones.

    find /System/Library/Frameworks/Python.framework/Versions/2.6/ -name "*.so"
    
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/X11/xcb/xcb.0.0.0.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/X11/xcb/xcb.0.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/X11/xcb/xcb.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/CoreGraphics/_CoreGraphics.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/OpenSSL/SSL.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/OpenSSL/crypto.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/OpenSSL/rand.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_appmain.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_carbon.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_inlines.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_nsbezierpath.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_nsbitmap.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_nsfont.so
     /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_nsquickdrawview.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_nsview.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/AppKit/_nswindow.so
    /System/Library/Frameworks/Python.framework/Versions/2.6//Extras/lib/python/PyObjC/CFNetwork/_manual.so