Main Content

Python Setup Script to Install MATLAB Engine API

MATLAB® provides a standard Python® setup.py file for building and installing the engine using Python setuptools. You can use the MATLAB setup.py script on Windows®, macOS, or Linux® platforms from the MATLAB command prompt or the operating system prompt.

To install the API using setup.py, you must call the script in the MATLAB engine API for Python folder, as shown in the following commands. To call the script from the system prompt, you need the path to the MATLAB folder. Start MATLAB and type matlabroot in the command window. Then replace matlabroot with the returned path value in the command.

For information about starting MATLAB from Python, see Start and Stop MATLAB Engine for Python.

Install on Windows

Choose one of these commands:

  • At a Windows operating system prompt (you might need administrator privileges to execute these commands), type:

    cd "matlabroot\extern\engines\python"
    python setup.py install
    
  • At the MATLAB command prompt, type:

    cd (fullfile(matlabroot,'extern','engines','python'))
    system('python setup.py install')
  • To use a nondefault folder, see Install MATLAB Engine API for Python in Nondefault Locations.

Install on Linux

Choose one of these commands:

  • At a Linux system prompt (you might need administrator privileges to execute these commands), type:

    cd "matlabroot/extern/engines/python"
    python setup.py install
    
  • At the MATLAB command prompt, type:

    cd (fullfile(matlabroot,'extern','engines','python'))
    system('python setup.py install')
  • To use a nondefault folder, see Install MATLAB Engine API for Python in Nondefault Locations.

Install on macOS

Choose one of these commands:

  • At a macOS terminal prompt (you might need administrator privileges to execute these commands), type:

    cd "matlabroot/extern/engines/python"
    python setup.py install
    
  • At the MATLAB command prompt, type:

    cd (fullfile(matlabroot,'extern','engines','python'))
    system('python setup.py install')
  • To use a nondefault folder, see Install MATLAB Engine API for Python in Nondefault Locations.

Install for Multiple MATLAB Versions

Each MATLAB release has a Python setup.py package. When you use the package, it runs the specified MATLAB version. To switch between MATLAB versions, you need to switch between the Python packages.

You can specify a MATLAB version to run from a Python script by installing the MATLAB Python packages to version-specific locations. For example, suppose that you want to call either MATLAB R2021b or R2022a from a Python version 3.9 script. You can install the engine API into local folders named matlab21bPy39 and matlab22aPy39. The steps are:

  1. Navigate to the MATLAB R2021b engine API for Python folder.

  2. Call setup.py to install to a local folder named matlab21bPy39.

  3. Navigate to the MATLAB R2022a folder.

  4. Call setup.py to install to a local folder named matlab22aPy39.

From a Windows system prompt, type:

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"

From a Linux system prompt, type:

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"

From a macOS terminal, type:

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"

To start each MATLAB version, see Start Specific MATLAB Engine Version.

Related Topics