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 a specific 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.

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 R2019a or R2019b from a Python version 3.6 script. You can install the engine API into local folders named matlab19aPy36 and matlab19bPy36. The steps are:

  1. Navigate to the MATLAB R2019a Python engine folder.

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

  3. Navigate to the MATLAB R2019b folder.

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

From a Windows system prompt, type:

cd "c:\Program Files\MATLAB\R2019a\extern\engines\python" 
python setup.py install --prefix="c:\work\matlab19aPy36"
cd "c:\Program Files\MATLAB\R2019b\extern\engines\python" 
python setup.py install --prefix="c:\work\matlab19bPy36"

From a Linux system prompt, type:

cd "/usr/local/MATLAB/R2019a/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19aPy36"
cd "/usr/local/MATLAB/R2019b/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19bPy36"

From a macOS terminal, type:

cd "/Applications/MATLAB_R2019a.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19aPy36"
cd "/Applications/MATLAB_R2019b.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19bPy36"

Related Topics