Friday, July 8, 2011

Installing mlabwrap on OS-X and Linux

Mlabwrap is an excellent Python wrapper for Matlab, allowing you to use Matlab functionality from within Python scripts. I recently tried to install it on OS-X (10.6) and Linux (11.4). I ran into minor issues with both OS'.

The first is that the setup.py that ships with version 1.1 of mlabwrap has a minor compile error related to the definition of a datatype. In my case this was fixed by changing:

# setup.py line 13
MATLAB_VERSION = None

to:
MATLAB_VERSION = 7.3

which tells mlabwrap to look for Matlab versions 7.3 and on. I made this change on both OS-X and Linux.

The second issue was with OS-X only, and relates to how mlabwrap references the Matlab shared libraries. Although mlabwrap builds successfully, import errors related to "@loader_path/libeng.dylib" and "@loader_path/libmx.dylib" occur.

I fixed this using the "install_name_tool" command to modify the mlabrawmodule.so library from mlabwrap so that it uses the absolute paths to these shared libraries:

The commands are:
sudo install_name_tool -change "@loader_path/libmx.dylib" "/Applications/Matlab_R2011a_Student/bin/maci64/libmx.dylib" mlabrawmodule.so


sudo install_name_tool -change "@loader_path/libeng.dylib" "/Applications/Matlab_R2011a_Student/bin/maci64/libeng.dylib" mlabrawmodule.so

Note that these commands assumes you are in the site-packages directory of your Python installation (or wherever you have install mlabwrap). You will also need to change the change the "/Matlab_R2011a_Student" portion of the paths to suit your Matlab installation.

5 comments:

  1. hi James

    I am using a OS-X 10.6 and I successfully installed mlabwrap following your suggestions. Post installation I did:
    sudo install_name_tool -change "@loader_path/libmx.dylib" "/Applications/Matlab_R2010a/bin/maci64/libmx.dylib" /Library/Python/2.6/site-packages/mlabrawmodule.so

    sudo install_name_tool -change "@loader_path/libeng.dylib" "/Applications/Matlab_R2010a/bin/maci64/libeng.dylib" /Library/Python/2.6/site-packages/mlabrawmodule.so

    however, when I 'from mlabwrap import mlab', I get:
    Traceback (most recent call last):
    File "", line 1, in
    File "mlabwrap.py", line 188, in
    import mlabraw
    ImportError: dlopen(/Library/Python/2.6/site-packages/mlabrawmodule.so, 2): Library not loaded: /Applications/Matlab_R2010a/bin/maci64/libeng.dylib
    Referenced from: /Library/Python/2.6/site-packages/mlabrawmodule.so
    Reason: image not found

    Is there anything thing that you can suggest I check out?
    Rethabile Khutlang

    ReplyDelete
  2. Hi,

    Sorry for the delay in responding. I would check that the file 'libeng.dylib' exists in the path /Applications/Matlab_R2010a/bin/maci64/libeng.dylib, if not then that is likely your problem. However I didn't encounter this issue myself so it's hard to be more helpful than that.

    Good luck,

    James

    ReplyDelete
  3. Hi James

    It works! I checked that file 'libeng.dylib' existed and it did. Then when 'import mlabraw' for the first time, it wanted matlab license and it worked.

    thanks

    ReplyDelete
  4. Hi Jmaes,

    I also followed your approach, and changed the path to libeng.dylib, libmat.dylib, and libut.dylib.


    But at the end, I receive the following error which I am not sure how to fix it:


    Library not loaded: libtbb.dylib
    Referenced from: /Applications/MATLAB_R2010b.app/bin/maci64/libut.dylib
    Reason: image not found

    Do you have any idea how to solve this problem?

    Thanks a lot

    ReplyDelete
  5. Hi James,

    Thanks for the instructions. Your suggestion to edit the setup.py file helped me install mlabwrap. However, I found I did not have to use the install_name_tool to point to the .dylib files. The mlabwrap website (http://mlabwrap.sourceforge.net/#os-x) recommends running

    >export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH$:/Applications/MATLAB701/bin/mac/
    >export MLABRAW_CMD_STR="/Applications/MATLAB701/bin/matlab -nodesktop"

    prior to running matlab. In my install of Matlab 2010b, the mac directory does not exist, but maci64 does. So I added

    >export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH$:/Applications/MATLAB_R2010b.app/bin/maci64/
    >export MLABRAW_CMD_STR="/Applications/MATLAB_R2010b.app/bin/matlab -nodesktop"

    to my .bash_profile. This did the trick. When I import mlabwrap I don't run into any errors.

    Thanks for your help. I probably would not have figured this out without your blog posting.

    ReplyDelete