Starting the Matlab engine in anaconda virtual environment returns "Segmentation error (kernel reset)"

I installed the official MATLAB Engine, following the instructions from the answer on Anaconda, to install the Matlab Engine on Linux in the Anaconda virtual environment with Python3.5. Now I can import matlab and matlab.engine without errors. However, when I try: matlab.engine.start_matlab() , I get a "Segmentation error (dropping the kernel)"

I tried to set LD_LIBRARY_PATH from conda (in case this is even relevant): export LD_LIBRARY_PATH=/System/Library/Frameworks/Python.framework/Versions/Current/lib:$LD_LIBRARY_PATH , but to no avail. The path does not exist, as far as I know, so I also tried export DYLD_LIBRARY_PATH=path_to_anaconda3/envs/myEnv/lib:$LD_LIBRARY_PATH

So, how can I run the Matlab script / call the Matlab scripts from Python from the Anaconda virtual environment?

I'm on Ubuntu by the way

+1
source share
1 answer

Short answer: there were two problems that needed to be fixed.

  • $LD_LIBRARY_PATH must not contain the path to install Anaconda. Adding such a path is not recommended according to the conda documentation: https://conda.io/docs/building/shared-libraries.html , but some packages may somehow do this, causing a segmentation error.
  • A symlink is needed from the libpythonXXX.dylib file of the correct version in / usr / lib /, so MATLAB can find the correct Python

Long answer: complete installation instructions for using MATLAB Engine with Anaconda

  • Install the MATLAB version that supports the Python you want to use. Make sure this particular MATLAB installation is activated.
  • Open a terminal and go to the folder containing the Python installation mechanism of MATLAB: cd "/usr/local/MATLAB/R2017a/extern/engines/python"
  • Run setup.py with the version of Python you want to use and the Anaconda environment location prefix: sudo python3.5 setup.py install --prefix="/your_path_to_anaconda3/envs/your_env" . At this point, you should be able to import matlab and matlab.engine from Python in your Anaconda environment, but in my case, starting the engine gave a segmentation error.
  • Locate the libpython file for the version you need. Your Anaconda environment should contain it: find /your_path_to_anaconda3/envs/your_env/ -name libpython* . In my case, it came back:
    • /.../Lib/libpython3.so
    • /.../Library/python3.5/configuration-3.5m/libpython3.5m.a
    • /.../Lib/libpython3.5m.so.1.0
    • /.../Lib/libpython3.5m.so
  • As I wanted to use it with python 3.5, I went with libpython3.5m (I don't know why "m" exists). Make a symlink from the .dylib version of this file to / usr / lib: sudo ln -s /your_path_to_anaconda3/envs/your_env/lib/libpython3.5m.dylib /usr/lib . Please note that you can have only one link called libpython3.5m.dylib in / usr / lib. Therefore, if you have multiple Anaconda environments using the same version of Python, you only need to set this link once, depending on which one. Remember not to delete this environment, as this will break the connection for all other environments that rely on it.
  • Launch a new terminal (!) And activate the Anaconda environment: source activate your_env . Check in your Anaconda environment if there are LD_LIBRARY_PATH references to the Anaconda environment echo $LD_LIBRARY_PATH . If so, make sure it doesn't work anymore: export LD_LIBRARY_PATH=only_paths_you_do_want_to_keep_separated_by_a_colon . This export action needs to be repeated whenever you activate the Anaconda environment, so you might want to take a look at the more permanent means of setting it up. However, in my case (besides the fact that I added it myself in the hope that this will improve the situation), the path was actually added by pygpu, so I ended up dumping LD_LIBRARY_PATH from my python script (until I noticed bad effects).
0
source

All Articles