在Anaconda虚拟环境中启动Matlab引擎会返回“分段错误(核心已转储)" [英] Starting Matlab engine in anaconda virtual environment returns 'Segmentation fault (core dumped)'
问题描述
我已经按照 Anaconda的答案中的说明安装了官方的MATLAB Engine将Linux上的Matlab引擎安装到运行Python3.5的Anaconda虚拟环境中.现在,我可以导入matlab
和matlab.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虚拟环境中从Python启动matlab引擎/调用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
-
$LD_LIBRARY_PATH
应该不包含Anaconda安装的路径.根据conda文档,不建议添加这样的路径: https://conda.io/docs/building/shared-libraries.html ,但某些软件包仍可能这样做,从而导致分段错误. - 需要从正确版本的libpythonXXX.dylib文件到/usr/lib/的符号链接,以便MATLAB可以找到正确的Python
$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.- 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 matlab
和matlab.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 toimport matlab
andmatlab.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
这篇关于在Anaconda虚拟环境中启动Matlab引擎会返回“分段错误(核心已转储)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!