Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

python - Starting Matlab engine in anaconda virtual environment returns 'Segmentation fault (core dumped)'

I've installed the official MATLAB Engine by following the instructions from the answer to Anaconda install Matlab Engine on Linux to an Anaconda virtual environment running Python3.5. I can now import matlab and matlab.engine without receiving errors. However, when I try: matlab.engine.start_matlab(), I get 'Segmentation fault (core dumped)'

I've tried setting LD_LIBRARY_PATH from within the conda environment (in case that is even relevant): export LD_LIBRARY_PATH=/System/Library/Frameworks/Python.framework/Versions/Current/lib:$LD_LIBRARY_PATH, but to no avail. The path doesn't exist either as far as I'm aware, so I've also tried export DYLD_LIBRARY_PATH=path_to_anaconda3/envs/myEnv/lib:$LD_LIBRARY_PATH

So how can I start the matlab engine/call Matlab scripts from Python from within a Anaconda virtual environment?

I'm on Ubuntu, by the way

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  1. $LD_LIBRARY_PATH should not contain a path to the Anaconda installation. Adding such a path is discouraged according to the conda documentation: https://conda.io/docs/building/shared-libraries.html, but some packages may do so anyways, causing the segmentation error.
  2. A symbolic link is needed from a libpythonXXX.dylib file of the right version to /usr/lib/, so that MATLAB can find the right Python

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

  • Install a MATLAB version that supports the Python you want to use. Ensure that this particular MATLAB installation is activated
  • Open a terminal and go to the folder containing the Python engine of the MATLAB installation: cd "/usr/local/MATLAB/R2017a/extern/engines/python"
  • Run setup.py with the Python version you want to use, and prefix the Anaconda environment location: 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 within the Python of your Anaconda environment, but, in my case, starting the engine gave the segmentation error.
  • Find the libpython file of the right version. Your Anaconda environment should contain it: find /your_path_to_anaconda3/envs/your_env/ -name libpython*. In my case this returned:
    • /.../lib/libpython3.so
    • /.../lib/python3.5/config-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 the 'm' is there). Make a symbolic link 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. Note that you can only have one link called libpython3.5m.dylib in /usr/lib. So if you have multiple Anaconda environments using the same version of Python, you only need to set up this link once to whichever one. Remember not to delete this environment though, as that would break the link for all other environments relying on it.
  • Start a new terminal (!) and activate your Anaconda environment: source activate your_env. Check within your Anaconda environment whether LD_LIBRARY_PATH contains any references to the Anaconda environment echo $LD_LIBRARY_PATH. If so, ensure that it no longer does: 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 your Anaconda environment, so you may want to look into more permanent means of setting it. However, in my case (apart from the fact I had been adding it myself in the hope this would improve things) the path was actually added by pygpu, so I ended up resetting LD_LIBRARY_PATH from my python script (so far without noticing ill effects).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...