MATLAB: Does MATLAB Engine for Python crash when using a non system default version of Python on MAC

MATLAB

I have downloaded the latest Python 2.7.x version from http://www.python.org/ for Mac OS X 10.10 and installed it through its default installer in the default location. I then Installed MATLAB Engine for Python in this Python version; this completed without errors. When I try to use the Engine however, Python crashes on the line:
import matlab.engine
The error message which is displayed is:
Segmentation fault: 11

Best Answer

MATLAB Engine for Python depends on a shared library matlabengineforpython2_7.so which in turn depends on libpython2.7.dylib. This libpython2.7.dylib is loaded as run-path-relative library (@rpath). In most Mac OS X setups this will mean that /usr/lib/libpython2.7.dylib will be used at runtime. This particular library is the system default library however, it is not updated or changed by the installers downloaded from http://www.python.org/. Meaning that there will be a mismatch between your Python interpreter version and the libpython2.7.dylib loaded by MATLAB Engine for Python. This then leads to a segmentation fault.
To resolve this issue we need to make sure that MATLAB Engine for Python can find the correct version of libpython2.7.dylib. To accomplish this add the lib directory of the correct Python version to the DYLD_LIBRARY_PATH. Assuming that Python was installed in its default you should for example be able to run the following in a Terminal to start Python with the correct environment:
export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/2.7/lib:$DYLD_LIBRARY_PATH
python