使用以下命令在终端中安装matlab python包之后:
cd "matlabroot\extern\engines\python"
python setup.py install
试图运行它,我得到一个段错误:
:~$ 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
但是,我可以通过设置matLD.engine工作的DYLD_LIBRARY_PATH来解决这个问题:
:~$ 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()
然而,当我之后尝试启动iPython时,我收到此错误:
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
正如您所看到的,python版本是不同的。我认为这是我的系统Python和Anaconda之间的冲突,但我不确定如何解决它,任何帮助非常感谢。 感谢。
答案 0 :(得分:0)
您需要使用conda命令行安装实用程序而不是python来安装库。 conda help search
和conda help install
可以帮助你。
答案 1 :(得分:0)
这不是一个直接的解决方案,但我遇到了同样的问题,并使用了这种解决方法。 我应该说我最终在我的Python环境中运行matlab.engine
,但它只是简单的糟糕。它无法在其他文件夹中运行带有嵌入式脚本的脚本,我也经历过无法找到一些内置函数。以下是针对Unix机器实现的,但我认为不需要进行大量修改就可以在Windows中运行。
这就是我做的事情:
在我的情况下,我需要对矩阵 X 执行一系列矩阵运算,并返回矩阵 S 。矩阵运算需要使用特定的Matlab函数。我的第一个想法是用matlab.engine
打开一个Matlab会话,然后在Python中管理矩阵操作,只在需要时调用Matlab函数。相反(作为子弹状态)我写了一个Matlab函数ComputeSimilarityMat
,它采用 X ,执行所有必要的操作,并返回 S 。然后我基本上只是使用子进程从Python运行Matlab函数。
通过子进程管理Matlab脚本的Python脚本如下所示:
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
我必须说,我确信有一种更好的方法可以将对象传递给Matlab然后必须像这样保存和加载,但它对我有用。仍然希望改进matlab.engine
但是在那之前我会采用这种方法。
注意:要从命令行/子进程运行Matlab,您需要将Matlab bin文件夹添加到PATH变量中,如下所示:
export PATH="/Applications/MATLAB_R2015a.app/bin:$PATH"
我把它放在我的.profile文件中。
答案 2 :(得分:0)
在我的情况下,将内容添加到LD_LIBRARY_PATH(Ubuntu的DYLD_LIBRARY_PATH版本)只会让事情变得更糟。相反,我必须确保它没有引用Python安装,并且必须添加来自/ usr / lib的符号链接。见https://stackoverflow.com/a/45161288/2524427