“import matlab.engine”适用于linux命令行,但不适用于Spyder

时间:2017-10-27 19:57:51

标签: ipython python-3.4 spyder matlab-engine

python(r2016a)的matlab引擎似乎已安装并使用python。我可以从bash提示符执行以下操作:

str

接下来我启动Spyder(从同一个bash提示符输入“spyder&”)这就是我在Spyder中尝试同样的事情:

$ python
Python 3.4.5 |Anaconda 4.3.1 (64-bit)| (default, Jul  2 2016, 17:47:47) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matlab.engine
>>> eng = matlab.engine.start_matlab()
>>> eng.abs(-1)
1
>>> exit()

使用IPython而不是python可以得到类似的结果,但错误信息量较少。很明显,Spyder找不到模块Python 3.4.5 |Anaconda 4.3.1 (64-bit)| (default, Jul 2 2016, 17:47:47) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import matlab.engine Traceback (most recent call last): File "/home/XXX/anaconda3/envs/mr2/lib/python3.4/site-packages/matlab/engine/__init__.py", line 42, in <module> pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION) File "/home/XXX/anaconda3/envs/mr2/lib/python3.4/importlib/__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 2254, in _gcd_import File "<frozen importlib._bootstrap>", line 2237, in _find_and_load File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked ImportError: No module named 'matlabengineforpython3_4' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/XXX/anaconda3/envs/mr2/lib/python3.4/site-packages/matlab/engine/__init__.py", line 58, in <module> pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION) File "/home/XXX/anaconda3/envs/mr2/lib/python3.4/importlib/__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 2254, in _gcd_import File "<frozen importlib._bootstrap>", line 2237, in _find_and_load File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible File "<frozen importlib._bootstrap>", line 539, in _check_name_wrapper File "<frozen importlib._bootstrap>", line 1715, in load_module File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed ImportError: /opt/local/matlab2016a/extern/engines/python/dist/matlab/engine/glnxa64/../../../../../../../bin/glnxa64/libicuio.so.54: undefined symbol: _ZN6icu_5413UnicodeString9doReplaceEiiPKDsii During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/XXX/anaconda3/envs/mr2/lib/python3.4/site-packages/matlab/engine/__init__.py", line 61, in <module> 'MathWorks Technical Support for assistance: %s' % e) OSError: Please reinstall MATLAB Engine for Python or contact MathWorks Technical Support for assistance: /opt/local/matlab2016a/extern/engines/python/dist/matlab/engine/glnxa64/../../../../../../../bin/glnxa64/libicuio.so.54: undefined symbol: _ZN6icu_5413UnicodeString9doReplaceEiiPKDsii >>> ,但我不知道从那里去哪里。

如何从spyder中正确使用MATLAB引擎?

1 个答案:

答案 0 :(得分:0)

此问题可能是由于MATLAB附带的libstdc ++与spyder链接的系统附带的libstdc ++之间不兼容。

“未找到CXXABI_1.3.9”错误消息表明MATLAB附带的libstdc ++。so.6库缺少系统绘制图形内容所需的ABI版本。

这是因为与MATLAB一起打包的这个特定库的版本比新的Linux操作系统附带的版本旧,这会导致兼容性问题。

您可以尝试以下变通方法:1。重命名libstdc ++。so.6库文件,以便MATLAB无法找到它并被迫使用系统的库版本。该文件位于matlabroot / sys / os / glnxa64中。将它重命名为libstdc ++。so.6.old就足够了。 (其中“matlabroot”是MATLAB的根安装目录)

  1. 通过将$ LD_PRELOAD环境变量设置为较新版本的libstdc ++,强制MATLAB加载系统版本。
  2. 此外,由于您尝试从MATLAB调用外部程序,这可能是第三方问题。但是,您可以尝试在“LD_LIBRARY_PATH”系统变量中正确设置库路径。

    find / -name“libstdc ++。so *”

    我发现用命令LD_PRELOAD = / usr / lib64 / libstdc ++调用matlab。so.6 matlab -desktop似乎解决了这个问题

    为了避免每次我在.bashrc文件别名上分配别名时必须输入此命令别名matlab =“LD_PRELOAD = / usr / lib64 / libstdc ++。so.6 / usr / local / bin / matlab -desktop “

    https://cn.mathworks.com/matlabcentral/answers/329796-issue-with-libstdc-so-6