Main Content

Install MATLAB Engine API for Python

MATLAB® Engine API for Python® allows you to call MATLAB functions and execute MATLAB commands from within a Python environment. To use the MATLAB engine, you must have a supported version of Python installed on your machine, and you must install MATLAB Engine API for Python as a Python package.

Verify Your Configuration

If you already have Python installed, verify that you are using a version that is supported by the MATLAB engine. You can check which version of Python you have installed by entering python -V at your operating system prompt. Then determine if your Python version is compatible with your MATLAB release by checking the Versions of Python Compatible with MATLAB Products by Release page.

In addition, verify that you are using a 64-bit version of Python. A 64-bit version of Python is necessary to match the architecture of MATLAB. To test whether your version of Python is 32-bit or 64-bit, enter this code at the Python prompt. This code returns True if the version is 64-bit and False if the version is 32-bit.

import sys 
print(sys.maxsize > 2**32)

If you do not yet have Python installed or need a different version, see Configure Your System to Use Python.

Ways to Install MATLAB Engine API for Python

MATLAB provides various methods for installing MATLAB Engine API for Python.

Install from MATLAB

You can install the MATLAB engine directly from MATLAB. Start MATLAB and run the following commands.

SystemMATLAB Commands

Windows®

cd (fullfile(matlabroot,"extern","engines","python"))
system("python -m pip install .")

Linux® and macOS

cd (fullfile(matlabroot,"extern","engines","python"))
system("python3 -m pip install .")

Install from Operating System

Alternatively, you can install the MATLAB engine from the operating system prompt. First, you need the path to the MATLAB Engine API for Python folder. To locate this folder, start MATLAB and enter matlabroot in the Command Window. Then replace matlabroot in the following commands with the path value that MATLAB returned.

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

SystemSystem Commands

Windows

cd "matlabroot\extern\engines\python"
python -m pip install .

Linux and macOS

cd "matlabroot/extern/engines/python"
python3 -m pip install .

Install from Python Package Index

Alternatively, you can install the MATLAB engine from the Python Package Index (PyPI). Unlike the other install options, you do not need to navigate to the MATLAB Engine API for Python folder first. To install from PyPI, run this command at your operating system prompt.

python -m pip install matlabengine 
This command installs the latest version of MATLAB Engine API for Python that is available in the Python Package Index. If you do not have the latest release of MATLAB, you need to specify a version of the MATLAB engine that is compatible with your release. You can determine a compatible version of the MATLAB engine by using the PyPI page for MATLAB Engine API for Python: From the Release history tab, review the Required MathWorks Products section for each MATLAB engine release.

Where to Install MATLAB Engine API for Python

You can install from MATLAB, the operating system, or the Python Package Index, and you can install to the default location under your Python interpreter, another nondefault location, or a virtual environment. By default, the installer builds MATLAB Engine API for Python in the matlabroot/extern/engines/python folder. The installer then installs the MATLAB engine in the default Python folder. If you use the installation methods in the Install from MATLAB, Install from Operating System, or Install from Python Package Index sections, then the MATLAB engine builds and installs in the default locations.

Install in Nondefault Folder Using setup.py

If you do not have write permission for the default install folder or want to install the MATLAB engine in a different folder, you can specify a different location.

To build and install the engine in a nondefault folder builddir, run these commands from your operating system prompt.

cd "matlabroot/extern/engines/python"
python setup.py build --build-base="builddir"
To include builddir on the search path for Python packages, add builddir to the PYTHONPATH environment variable.

Install in Nondefault Folder Using Python Package Index

To install the engine in a nondefault folder installdir, run this command from your operating system prompt. This installation method uses the Python Package Index.

python -m pip install --target installdir matlabengine

To include installdir on the search path for Python packages, add installdir to the PYTHONPATH environment variable.

Install in Virtual Environment

You can install MATLAB Engine API for Python in a virtual environment. For detailed instructions on installing MATLAB Engine API for Python in a virtual environment, see the MATLAB Answers™ article Use the MATLAB Engine API for Python with a Virtual Environment. For information about virtual Python environments, see the Python tutorial Virtual Environments and Packages. You must activate the virtual environment before running the installation commands.

Start MATLAB Engine in Python

To import the MATLAB Engine API for Python package and start the MATLAB engine, run these commands from the Python prompt.

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

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

See Also

Topics

External Websites