关键字
MacOS, MATLAB Engine, Python 2.7, Python 3.6, Permission Denied,Administrator
简介
在Mac OS X下,对Anaconda中的Python 3.6(而非Mac OS自带的Python 2.7)安装MATLAB Engine API,以实现Python对MATLAB函数的调用。
具体实现
基础方法见:
https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html
我采用的是上述地址中在MATLAB command promt进行操作的方法,非terminal。
MATLAB command promt中直接输入以下命令:
cd (fullfile(matlabroot,'extern','engines','python'))
system('python setup.py install')`
一开始我得到了如下报错信息:
Permission denied, 意味着没有权限write到该路径。
为了修改权限,确保自己是administrator的用户(否则没有修改权限的权限),然后按照下面的方法修改写入权限(write&read):
https://support.apple.com/zh-cn/guide/mac-help/mchlp1203/mac
有一个trick是最好直接把整个文件夹的权限一起修改,不然可能缺少其他文件写入权限。方法如下(包含在上述网址中):
经过了这一步,再次运行:
cd (fullfile(matlabroot,'extern','engines','python'))
system('python setup.py install')
我得到了如下结果:这说明已经成功安装MATLAB Engine for Python了。
但是,在Jupyter Notebook里面仍然不能运行,显示没有matlab module。
这是因为我们是给MacOS自带的Python2.7 安装了engine,而非conda中的Python3.7。
为了给Python3.7安装engine,首先找到Python的位置,可以在terminal用:
which python
实现。
我的安装位置是:
/Users/Jayson/opt/anaconda3/bin/python
把/Users/Jayson/opt/anaconda3/bin/python
中的python改成相应的安装地址即可。
完整命令如下:
>> cd (fullfile(matlabroot,'extern','engines','python'))
system('python setup.py install')
完成。
结语
第一篇博客,非常的简单,但是并没有在网上找到相应的资料,所以mark一下,希望可以帮助到更多的人。
来源:CSDN
作者:Jayson_J
链接:https://blog.csdn.net/weixin_41187146/article/details/104219852