原文链接: https://www.computationalimaging.cn/2019/10/matlab-for-python.html
由于训练网络计算损失函数的时候需要进行FFT和稀疏矩阵,使用Python完成非常麻烦,因此希望能够在服务器上安装供Python使用的MATLAB Engine, 但有如下两个问题:
1. 普通MATLAB Engine for Python的安装需要root权限,但我没有;
2. 使用Conda安装了多个环境,如何控制指定环境安装Matlab Engine也是问题。
MATLAB官网给出了有用但不是很详细的解决方案:
By default, the installer builds the engine API for Python® in the
folder. The installer installs the engine in the default Python folder. If you do not have write permission for these folders, then select one of the following nondefault options. If you install in another folder, then update the
matlabroot
\extern\engines\pythonPYTHONPATH
variable to the location of that folder.Options for building and installing the engine API follow, along with the commands to enter at the operating system prompt.
Build in Nondefault Folder and Install in Default Folder
If you do not have write permission to build the engine in the MATLAB® folder, then use a nondefault folder,
builddir
.cd "matlabroot\extern\engines\python" python setup.py build --build-base="builddir" install
Build in Default Folder and Install in Nondefault Folder
If you do not have write permission to install the engine in the default Python folder, then use a nondefault folder,
installdir
.cd "matlabroot\extern\engines\python" python setup.py install --prefix="installdir"
To include
installdir
in the search path for Python packages, addinstalldir
to thePYTHONPATH
environment variable.Build and Install in Nondefault Folders
If you do not have write permission for both the MATLAB folder and the default Python folder, then you can specify nondefault folders. Use
builddir
for the build folder andinstalldir
for the install folder.cd "matlabroot\extern\engines\python" python setup.py build --build-base="builddir" install --prefix="installdir"
具体操作其实很简单:
首先进入matlab安装目录,可以通过whereis matlab简化步骤:
(base) XXX@gpu01:/usr/local/MATLAB/R2018a/extern/engines/python$
之后切换环境以保证安装在了你想要它安装的那个环境里:
conda activate python36
之后指定目录安装即可:
python3.6 setup.py build --build-base="/share/home/XXX/matlab/" install --prefix="/share/home/XXX/anaconda3/envs/python36"
其中,XXX即为用户名,这两个目录显然普通用户是有写入权限的。
验证成功: