Python Support
To call Python® modules in MATLAB®, you must have a supported version of the reference implementation [CPython] installed on your system. Install a distribution, such as those found at //www.python.org/downloads/. MATLAB does not support CPython versions installed from the Microsoft® store. For supported version information, see Versions of Python Compatible with MATLAB Products by Release. If you are on a Linux® or Mac platform, you already have Python installed. If you are on Windows®, you need to install a distribution, if you have not already done so. For more information, see Install Supported Python Implementation.
To verify that Python is installed on your system, open the Python interpreter from your system prompt and call Python functions.
By default, MATLAB selects the version of Python based on your system path. To view the system path in MATLAB, use the
getenv['path']
command. To determine which version MATLAB is using, call the pyenv
function.
The value set by pyenv
is persistent across MATLAB sessions. If you have multiple supported versions, use pyenv
to display the version currently used by MATLAB. MATLAB automatically selects and loads a Python version when you type a Python statement. For example, to call funcname
,
type:
To change versions:
If Python is loaded in
InProcess
ExecutionMode
in a single MATLAB session, then restart MATLAB and runpyenv
with the new version information.If Python is loaded in
OutOfProcess
mode, then callterminate
and runpyenv
with the new version information.
Install Supported Python Implementation
Access //www.python.org/downloads/ and scroll to the Looking for a specific release section.
Find the version you want and click Download. For supported version information, see Versions of Python Compatible with MATLAB Products by Release.
Click the format you want for the 64-bit version and follow the online instructions.
Note
To install version 2.7 for 64-bit MATLAB on Microsoft Windows systems, select the 64-bit Python version, called Windows x86-64 MSI installer.
If you get the error message Unable to resolve the name py.myfunc, you might have an installation problem.
Set Python Version on Windows Platform
On Windows platforms, use either:
pyenv['Version','version']
or
pyenv['Version','executable']
where executable
is the full path to the Python executable file.
Note
If you downloaded a Python interpreter, but did not register it in the Windows registry, use:
pyenv['Version','executable']
Download 64-Bit Version of Python on Windows Platforms
The architecture of Python must match the architecture of MATLAB. For more information, see Install Supported Python Implementation.
Set Python Version on Mac and Linux Platforms
To set the version, type:
pyenv['Version','executable']
where
executable
is the full path to the Python executable file.
Requirements for Building Python Executable
On Linux and Mac systems, if you build the Python executable, configure the build with the --enable-shared
option.
See Also
pyenv
Related Topics
- Versions of Python Compatible with MATLAB Products by Release
- Unable to resolve the name py.myfunc
External Websites
- //www.python.org/downloads/
Main Content
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 //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.
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 thePYTHONPATH
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
- System Requirements for MATLAB Engine API for Python
- Versions of Python Compatible with MATLAB Products by Release
- Install Supported Python Implementation
- Install MATLAB Engine API for Python in Nondefault Locations
- Start and Stop MATLAB Engine for Python
External Websites
- Python 2.7 Documentation — Installing Python Modules
- Trial Software
- Trial Software
- Product Updates
- Product Updates