Matlab Engine Python - OSx Anaconda Segfault or DYLD_LIBRARY_PATH error with iPython

After installing the python matlab package in the terminal with:

cd "matlabroot\extern\engines\python" python setup.py install 

And trying to run it, I get segfault:

 :~$ python Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, May 28 2015, 17:04:42) [GCC 4.2.1 (Apple Inc. build 5577)] on darwin Type "help", "copyright", "credits" or "license" for more information. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://binstar.org >>> import matlab.engine Segmentation fault: 11 

However, I can get around this by setting DYLD_LIBRARY_PATH, after which matlab.engine works:

 :~$ export DYLD_LIBRARY_PATH=/System/Library/Frameworks/Python.framework/Versions/Current/lib:$DYLD_LIBRARY_PATH :~$ python Python 2.7.6 (default, Sep 9 2014, 15:04:36) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import matlab.engine >>> eng = matlab.engine.start_matlab() >>> exit() 

However, when I try to start iPython, I get the following error:

 Traceback (most recent call last): File "//anaconda/bin/ipython", line 4, in <module> from IPython import start_ipython File "//anaconda/lib/python2.7/site-packages/IPython/__init__.py", line 45, in <module> from .config.loader import Config File "//anaconda/lib/python2.7/site-packages/IPython/config/__init__.py", line 6, in <module> from .application import * File "//anaconda/lib/python2.7/site-packages/IPython/config/application.py", line 19, in <module> from IPython.config.configurable import SingletonConfigurable File "//anaconda/lib/python2.7/site-packages/IPython/config/configurable.py", line 12, in <module> from .loader import Config, LazyConfigValue File "//anaconda/lib/python2.7/site-packages/IPython/config/loader.py", line 16, in <module> from IPython.utils.path import filefind, get_ipython_dir File "//anaconda/lib/python2.7/site-packages/IPython/utils/path.py", line 14, in <module> import tempfile File "//anaconda/lib/python2.7/tempfile.py", line 32, in <module> import io as _io File "//anaconda/lib/python2.7/io.py", line 51, in <module> import _io ImportError: dlopen(//anaconda/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyErr_ReplaceException Referenced from: //anaconda/lib/python2.7/lib-dynload/_io.so Expected in: dynamic lookup 

As you can see, python versions are different. I think this is a conflict between my Python system and Anaconda, but I'm not sure how to fix it, any help is much appreciated. Thanks.

+4
source share
3 answers

You need to install the libraries using the conda command line utility, not python. conda help search and conda help install should catch you.

0
source

This is not a direct solution, but I ran into the same problem and used this workaround. And I have to say that I finally made matlab.engine run in my Python environment, but that is just dragged in. He could not run scripts with embedded scripts in other folders, and I also experienced that he could not find some built-in functions. The following is implemented for a Unix machine, but it does not take much time to run on Windows.

Here is what I did :

  • Wrote the main Matlab script that could do everything I needed from Matlab.
  • Ran that script through a subprocess in Python .

In my case, I needed a series of matrix operations for the matrix X and return the matrix S. Matrix operations required the use of a specific Matlab function. My first idea was to open a Matlab session with matlab.engine , and then control the matrix operations in Python, only calling the Matlab function if necessary. Instead (as a state of bullets) I wrote the Matlab ComputeSimilarityMat function, which takes X , performs all the necessary operations, and returns S. Then I basically just ran this Matlab function from Python using a subprocess.

This is what the Python script controlling the Matlab script through the subprocess looks like:

 import subprocess import numpy as np def run_matlab(X): """Produce trait-similarity matrix S from a trait-space matrix X Parameters ---------- X : numpy.ndarray Returns ------- S : numpy.ndarray """ # Dump input in .csv, so Matlab can load it np.savetxt('X.csv', X, delimiter=",") # Code executed in Matlab. Important to end with quit; matlab_code = "X=csvread('X.csv');" \ "S=ComputeSimilarityMat(X);" \ "csvwrite('S.csv',S);" \ "quit;" # -nosplash suppresses the splash window on startup matlab_call = ["matlab", "-nodesktop", "-nosplash", "-r", matlab_code] subprocess.call(matlab_call) # Load the .csv file that Matlab dumps after finishing S = np.genfromtxt('X.csv', delimiter=",") return S 

I have to say that I'm sure there is a better way to pass the Matlab object and then save and load like this, but it worked for me. Still hoping for an improvement on matlab.engine , though, but until then I'm going with this approach.

Note. . To start Matlab from the command line / subprocess, you need to add the Matlab bin folder to your PATH variable as follows:

 export PATH="/Applications/MATLAB_R2015a.app/bin:$PATH" 

I just put this in my .profile file.

0
source

In my case, adding things to LD_LIBRARY_PATH (Ubuntu version DYLD_LIBRARY_PATH) only made things worse. Instead, I had to make sure that this was not the case for installing Python, and instead I had to add a symlink from / usr / lib. See fooobar.com/questions/1601962 / ...

0
source

All Articles