Main Content

Install MATLAB Engine API for Python

To start the MATLAB® engine within a Python® session, you first must install the engine API as a Python package.

Verify Your Configuration

Before you install, verify your Python and MATLAB configurations.

  • Check that your system has a supported version of Python and MATLAB R2014b or later. For more information, see Versions of Python Compatible with MATLAB Products by Release.

  • To check that Python is installed on your system, run Python at the operating system prompt.

  • Add the folder that contains the Python interpreter to your path, if it is not already there.

Install Engine API

You can install the MATLAB Engine API for Python using the pip command or a Python setup script setup.py.

Install Using pip

Starting with MATLAB R2022b, you can use the pip command to install the API. Choose one of the following procedures and execute from the system prompt.

  • To install from the MATLAB folder, on Windows® type:

    cd "matlabroot\extern\engines\python"
    python -m pip install .
  • Install the engine API from https://pypi.org/project/matlabengine with the command:

    python -m pip install matlabengine

Install Using setup.py

MATLAB provides a standard Python setup.py file for building and installing the engine using Python setuptools. For platform-specific commands, see Python Setup Script to Install MATLAB Engine API.

Start MATLAB Engine

Start Python. Type these commands from the Python prompt to import the MATLAB module and start the engine:

import matlab.engine
eng = matlab.engine.start_matlab()

For more information, see Start and Stop MATLAB Engine for Python.

Troubleshooting MATLAB Engine API for Python Installation

  • Make sure that your MATLAB release supports your Python version. See Versions of Python Compatible with MATLAB Products by Release .

  • Make sure that you have administrator privileges to execute the install command from the operating system prompt. On Windows, open the command prompt with the Run as administrator option.

  • You must run the Python install command from the specified MATLAB folder. For detailed instructions, choose one of the platform links in Install Engine API.

    python setup.py install
    
  • The installer installs the engine in the default Python folder. To use a nondefault location, see Install MATLAB Engine API for Python in Nondefault Locations.

  • If you installed the package in a nondefault folder using --prefix, make sure to set the PYTHONPATH environment variable. For example, suppose that you used this installation command:

    python setup.py install --prefix="matlab19bPy36"
    

    In Python, update PYTHONPATH with this command:

    sys.path.append("matlab19bPy36")
  • For more troubleshooting information, see Troubleshoot MATLAB Errors in Python.

Related Topics

External Websites