Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

用于安装 MATLAB Engine API 的 Python 设置脚本

MATLAB® 提供了标准的 Python® setup.py 文件,用于使用 Python setuptools 编译和安装引擎。您可以在 Windows®macOS 或 Linux® 平台上的 MATLAB 命令提示符下或操作系统提示符下,使用 MATLAB setup.py 脚本。

要使用 setup.py 安装 API,您必须调用特定文件夹中的脚本,如以下命令所示。要从系统提示符调用脚本,您需要 MATLAB 文件夹的路径。启动 MATLAB,并在命令行窗口中键入 matlabroot。然后用命令中返回的路径值替换 matlabroot

Windows 上安装

选择以下命令之一:

  • 在 Windows 操作系统提示符下(您可能需要管理员特权才能执行这些命令),键入:

    cd "matlabroot\extern\engines\python"
    python setup.py install
    
  • 在 MATLAB 命令提示符下,键入:

    cd (fullfile(matlabroot,'extern','engines','python'))
    system('python setup.py install')
  • 要使用非默认文件夹,请参阅在非默认位置安装用于 Python 的 MATLAB Engine API

Linux 上安装

选择以下命令之一:

  • 在 Linux 系统提示符下(您可能需要管理员特权才能执行这些命令),键入:

    cd "matlabroot/extern/engines/python"
    python setup.py install
    
  • 在 MATLAB 命令提示符下,键入:

    cd (fullfile(matlabroot,'extern','engines','python'))
    system('python setup.py install')
  • 要使用非默认文件夹,请参阅在非默认位置安装用于 Python 的 MATLAB Engine API

macOS 上安装

选择以下命令之一:

  • macOS 终端提示符下(您可能需要管理员特权才能执行这些命令),键入:

    cd "matlabroot/extern/engines/python"
    python setup.py install
    
  • 在 MATLAB 命令提示符下,键入:

    cd (fullfile(matlabroot,'extern','engines','python'))
    system('python setup.py install')
  • 要使用非默认文件夹,请参阅在非默认位置安装用于 Python 的 MATLAB Engine API

为多个 MATLAB 版本安装

每个 MATLAB 版本都有一个 Python setup.py 包。当您使用该包时,它将运行指定的 MATLAB 版本。要在 MATLAB 版本之间切换,您需要在 Python 包之间切换。

通过将 MATLAB Python 包安装到特定于版本的位置,可以从 Python 脚本指定要运行的 MATLAB 版本。例如,假设您要从 Python 版本 3.9 脚本中调用 MATLAB R2021b 或 R2022a。您可以将 Engine API 安装到名为 matlab21bPy39matlab22aPy39 的本地文件夹中。步骤如下:

  1. 导航到 MATLAB R2021b Python 引擎文件夹。

  2. 调用 setup.py 以安装到名为 matlab21bPy39 的本地文件夹。

  3. 导航到 MATLAB R2022a 文件夹。

  4. 调用 setup.py 以安装到名为 matlab22aPy39 的本地文件夹。

从 Windows 系统提示符,键入:

cd "c:\Program Files\MATLAB\R2021b\extern\engines\python" 
python setup.py install --prefix="c:\work\matlab21bPy39"
cd "c:\Program Files\MATLAB\R2022a\extern\engines\python" 
python setup.py install --prefix="c:\work\matlab22aPy39"

从 Linux 系统提示符,键入:

cd "/usr/local/MATLAB/R2021b/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab21bPy39"
cd "/usr/local/MATLAB/R2022a/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab22aPy39"

macOS 终端,键入:

cd "/Applications/MATLAB_R2021b.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab21bPy39"
cd "/Applications/MATLAB_R2022a.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab22aPy39"

相关主题