在 anaconda 虚拟环境中启动 Matlab 引擎返回“分段错误(核心转储)"

问题描述

我已按照 Anaconda 答案中的说明安装了官方 MATLAB 引擎在 Linux 上安装 Matlab 引擎 到运行 Python3.5 的 Anaconda 虚拟环境.我现在可以导入 matlabmatlab.engine 而不会收到错误.但是,当我尝试:matlab.engine.start_matlab(),我得到分段错误(核心转储)"

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)'

我尝试在 conda 环境中设置 LD_LIBRARY_PATH(以防万一):export LD_LIBRARY_PATH=/System/Library/Frameworks/Python.framework/Versions/Current/lib:$LD_LIBRARY_PATH,但无济于事.据我所知,该路径也不存在,所以我也尝试过 export DYLD_LIBRARY_PATH=path_to_anaconda3/envs/myEnv/lib:$LD_LIBRARY_PATH

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

那么如何从 Anaconda 虚拟环境中启动 matlab 引擎/从 Python 调用 Matlab 脚本?

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

顺便说一句,我在 Ubuntu 上

I'm on Ubuntu, by the way


解决方案

简答:有两个问题需要解决

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

  1. $LD_LIBRARY_PATH 应该不包含 Anaconda 安装的路径.根据 conda 文档,不鼓励添加这样的路径:https://conda.io/docs/building/shared-libraries.html,但有些软件包可能会这样做,从而导致分段错误.
  2. 需要从正确版本的 libpythonXXX.dylib 文件到/usr/lib/的符号链接,以便 MATLAB 可以找到正确的 Python
  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

长答案:使用 MATLAB Engine 和 Anaconda 的完整安装说明

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

  • 安装支持您要使用的 Python 的 MATLAB 版本.确保此特定 MATLAB 安装已激活
  • 打开终端并转到包含 MATLAB 安装的 Python 引擎的文件夹:cd "/usr/local/MATLAB/R2017a/extern/engines/python"
  • 使用您要使用的 Python 版本运行 setup.py,并为 Anaconda 环境位置添加前缀:sudo python3.5 setup.py install --prefix="/your_path_to_anaconda3/envs/your_env".此时,您应该能够从 Anaconda 环境的 Python 中 import matlabmatlab.engine,但是,在我的情况下,启动引擎会导致分段错误.
  • 找到正确版本的 libpython 文件.您的 Anaconda 环境应该包含它:find/your_path_to_anaconda3/envs/your_env/-name libpython*.就我而言,这返回了:
    • /.../lib/libpython3.so
    • /.../lib/python3.5/config-3.5m/libpython3.5m.a
    • /.../lib/libpython3.5m.so.1.0
    • /.../lib/libpython3.5m.so
    • 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

相关文章