views:

697

answers:

2

I've compiled a Python module using my own Qt4 library located in ~/opt/qt-4.6.0/, but when I try to import that module, the dynamic libraries that get loaded are from my MacPorts Qt4 installation.

$ /opt/local/bin/python2.6
>>> import vtk
objc[58041]: Class QMacSoundDelegate is implemented in both /Users/luis/opt/qt-4.6.0/lib/QtGui.framework/Versions/4/QtGui and /opt/local/libexec/qt4-mac/lib/QtGui.framework/Versions/4/QtGui. Using implementation from /opt/local/libexec/qt4-mac/lib/QtGui.framework/Versions/4/QtGui.
objc[58045]: Class QCocoaColorPanelDelegate is implemented in both /Users/luis/opt/qt-4.6.0/lib/QtGui.framework/Versions/4/QtGui and /opt/local/libexec/qt4-mac/lib/QtGui.framework/Versions/4/QtGui. Using implementation from /opt/local/libexec/qt4-mac/lib/QtGui.framework/Versions/4/QtGui.
[... more output like above ...]
>>>

Is there a way of telling Python (also installed from MacPorts) to load the frameworks located in my ~/opt/qt-4.6.0/lib/ directory? I'm not sure what environment variables to change.

+2  A: 

Try setting the DYLD_LIBRARY_PATH to put your libraries in ~/opt/qt/... before the MacPorts' libraries before invoking python (take a look at ~/.profile for an example of how to do this if you don't know; MacPorts does the exact same thing to put its libraries on the DYLD_LIBRARY_PATH). dyld, the OS X dynamic linker uses DYLD_LIBRARY_PATH to find libraries at load time (among other methods); See man dyld for more info.

Barry Wark
I had tried LD_LIBRARY_PATH before, but it didn't work. Thanks a lot for the pointer to `dyld`, though. Its man page is very informative. Let's see if I can use that to resolve this issue.
Luis
Did you try DYLD_LIBRARY_PATH as well?
Barry Wark
...just saw your answer. Glad you figured it out.
Barry Wark
I just tried again with only DYLD_LIBRARY_PATH, but it wasn't sufficient. Frameworks must get loaded different than the usual .dylib's, hence the need for DYLD_FRAMEWORK_PATH.Thanks!
Luis
A: 

Ok, after Barry Wark pointed me to dyld(1), the man page described a number of variables that I could set.

The first hint came from setting the environment variable DYLD_PRINT_LIBRARIES, so I could see what libraries were being loaded.

$ DYLD_PRINT_LIBRARIES=1 python -c 'import vtk'
[... snip ...]
dyld: loaded: /opt/local/libexec/qt4-mac/lib/QtGui.framework/Versions/4/QtGui
dyld: loaded: /opt/local/lib/libpng12.0.dylib
dyld: loaded: /opt/local/libexec/qt4-mac/lib/QtSql.framework/Versions/4/QtSql
dyld: loaded: /opt/local/libexec/qt4-mac/lib/QtCore.framework/Versions/4/QtCore
[... snip ...]
dyld: loaded: /Users/luis/opt/qt-4.6.0/lib/QtGui.framework/Versions/4/QtGui
dyld: loaded: /Users/luis/opt/qt-4.6.0/lib/QtSql.framework/Versions/4/QtSql
dyld: loaded: /Users/luis/opt/qt-4.6.0/lib/QtCore.framework/Versions/4/QtCore
[... snip ...]
$

Ah, so the frameworks for qt4-mac were indeed being loaded first, just as we suspected. Rereading the man page, the next thing we can try is changing the DYLD_FRAMEWORK_PATH so that it knows where to look. I now added this line to the end of my ~/.bash_profile

export DYLD_FRAMEWORK_PATH="${HOME}/opt/qt-4.6.0/lib:${DYLD_FRAMEWORK_PATH}"

and after logging back in, we try importing the vtk python module again:

$ python -c 'import vtk'
$

There's no output this time. Issue fixed!

Luis