Import of matlab.engine works in IPython but not i

2019-02-21 02:37发布

I just installed the MATLAB Engine API for Python to my home directory using

$ python setup.py build --build-base=$HOME install --prefix=$HOME

However, when I try to import matlab.engine from a Jupyter notebook, I get the following error:

---------------------------------------------------------------------------
EnvironmentError                          Traceback (most recent call last)
<ipython-input-2-833daa01cd46> in <module>()
      8 import numpy as np
      9 import matplotlib.pyplot as plt
---> 10 import matlab.engine
     11 
     12 from dopa_net.behavioral_experiments.analysis_code import behavior_analysis as ba

/home/despo/dbliss/lib/matlab/engine/__init__.py in <module>()
     57         pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION)
     58     except:
---> 59         raise EnvironmentError('The installation of MATLAB Engine for Python is '
     60                                 'corrupted.  Please reinstall it or contact '
     61                                 'MathWorks Technical Support for assistance.')

EnvironmentError: The installation of MATLAB Engine for Python is corrupted.  Please reinstall it or contact MathWorks Technical Support for assistance.

Oddly, I don't get this error when I import matlab.engine from within an IPython shell in the same directory as the Jupyter notebook. Not only can I import the engine from the shell -- it works in the shell, which demonstrates to me that nothing is corrupted:

In [1]: import matlab.engine

In [2]: eng = matlab.engine.start_matlab()

In [3]: tf = eng.isprime(37)

In [4]: print tf
True

Any ideas what's going on?

N.B. I did install a version of the engine to my home directory earlier today, but I tried to (and thought I did) uninstall it. My guess is some part of it has been left behind and is causing trouble. But why is it causing trouble only for Jupyter, and not for the IPython shell? (And how would you guess I should get rid of it?)

Update

I tracked the error to the following line in the __init__.py file for the matlab.engine module:

importlib.import_module("matlabengineforpython2_7")

which does not raise an error in the IPython shell, but does raise the following error in Jupyter:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-831d184262c4> in <module>()
----> 1 importlib.import_module("matlabengineforpython2_7")

/usr/local/anaconda-2.4.0/lib/python2.7/importlib/__init__.pyc in import_module(name, package)
     35             level += 1
     36         name = _resolve_name(name[level:], package, level)
---> 37     __import__(name)
     38     return sys.modules[name]

ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by /srv/local/matlab-2015a/extern/engines/python/dist/matlab/engine/glnxa64/../../../../../../../bin/glnxa64/libmwfl.so)

I have no idea what's going on here.

1条回答
男人必须洒脱
2楼-- · 2019-02-21 03:21

The hypothesis that the IPython Shell and Jupyter Notebook don't have access to the same C libraries seems to have been correct. Why this is the case, I have no idea. The two were installed as part of the same package, use the same version of Python, etc.

Adding the following line to my .bashrc shell configuration file fixed the issue:

export LD_LIBRARY_PATH=XXX

where XXX is the directory containing the version of libstdc++.so.6 I want the Jupyter Notebook to use (and that the IPython Shell already uses).

查看更多
登录 后发表回答