使用 Anaconda 安装 "MATLAB Engine for Python" 时出现问题

Problems installing "MATLAB Engine for Python" with Anaconda

我正在使用 Python 3.4.3 | OS X 10.10.5 及 these instructions to install MATLAB Engine for Python 之后的 Anaconda 2.3.0 (x86_64)。安装似乎成功了,但在启动 python 并尝试导入时,我看到以下内容:

Python 3.4.3 |Anaconda 2.3.0 (x86_64)| (default, Oct 20 2015, 14:27:51) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matlab.engine
Traceback (most recent call last):
  File "/Users/tyler/.local/lib/python3.4/site-packages/matlab/engine/__init__.py", line 42, in <module>
    pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION)
  File "/Users/tyler/anaconda/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 "/Users/tyler/.local/lib/python3.4/site-packages/matlab/engine/__init__.py", line 58, in <module>
    pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION)
  File "/Users/tyler/anaconda/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: dlopen(/Applications/MATLAB_R2015b.app/extern/engines/python/dist/matlab/engine/maci64/matlabengineforpython3_4.so, 2):     Library not loaded: @rpath/libpython3.4m.dylib    
  Referenced from: /Applications/MATLAB_R2015b    .app/extern/engines/python/dist/matlab/engine/maci64/matlabengineforpython3_4.so    
  Reason: image not found    

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tyler/.local/lib/python3.4/site-packages/matlab/engine/__init__.py", line 60, in <module>
    raise EnvironmentError('The installation of MATLAB Engine for Python is '
OSError: The installation of MATLAB Engine for Python is corrupted.  Please reinstall it or contact MathWorks Technical Support for assistance.

在研究这个问题时,我发现 several other references to this exact issue on MathWorks' website, all without a resolution. The final link suggests the problem may be with dynamic linking

鉴于有多少科学工具箱仅在 MATLAB 中可用,如果能够使用 Anaconda 调用此引擎将非常有用。虽然还有其他支持此功能的软件包可用,但它们总是有限制或其他复杂性,如客户端-服务器架构。

欢迎提出任何建议。

根据我与 MathWorks 支持人员的沟通,Python 的 MATLAB 引擎与 Anaconda 不兼容。幸运的是,这是不正确的。

感谢 Anaconda 邮件列表的指导,我找到了一个简单的解决方案:

sudo ln -s ~/anaconda/lib/libpython3.4m.dylib /usr/lib

此后,matlab.engine导入成功。希望这对某人有所帮助!

编辑:从 OS X 10.11 El Capitan 开始,由于无根功能,需要调整解决方法,因为 root 不再具有修改 /usr/lib

的权限
sudo ln -s ~/anaconda/lib/libpython3.4m.dylib /usr/local/lib

如果您尝试在 conda 环境中安装 matlab 引擎,请按照以下步骤操作:

cd "matlabroot\extern\engines\python"
python setup.py install --prefix="installdir"

例如

cd /opt/MATLAB/R2019a/extern/engines/python
python setup.py install --prefix="/home/nagabhushan/anaconda3/"

现在,matlab引擎包其实是安装在单独的 anaconda3/lib/python3.6/site-package/matlab
因此,您需要手动将该 matlab 文件夹移动到您的 python 正在使用的站点包文件夹。如果您有多个虚拟环境,请将其移动到您将 运行 程序的虚拟环境的相应站点包文件夹中。 例如 anaconda3/envs/my_env/lob/python3.6/site-packages/matlab

参考文献:

编辑 1(2020 年 3 月 3 日):
我尝试激活我的环境,然后简单地安装 matlab 引擎,它成功了!我正在使用 Python 3.7

python setup.py install

请注意,这最初给出了 error: You do not have write permission in build/lib/matlab/engine/ 的错误,但递归地更改构建目录的权限有效:sudo chmod -R 777 build/