To call Python libraries from MATLAB, install a supported version of the reference implementation (CPython) for Python. MATLAB supports versions 2.7, 3.6, and 3.7. The version you install depends on the libraries you are using. For information, see Install Supported Python Implementation.
To call a Python function, type py.
in front of
the module name and function name. Pass MATLAB data as arguments to Python functions; MATLAB converts the data into types that best represent the data to the
Python language. For example, py.os.listdir('.')
lists
the contents of the current folder.
MATLAB automatically loads Python when you type py.
. You cannot change the interpreter after MATLAB loads Python. To change the interpreter, restart MATLAB, and then call command
pyenv
.
To call functions in third-party libraries that are not compatible with MATLAB, see Out-of-Process Execution of Python Functionality.
For help on the Python language, see www.python.org/doc
. For help on third-party or
user-defined modules, refer to the product documentation.
To call MATLAB functions from Python applications, see Calling MATLAB from Python.
pyenv | Change default environment of Python interpreter |
PythonEnvironment | Python environment information |
matlab.exception.PyException | Capture error information for Python exception |
System and Configuration Requirements
How to verify you have installed a supported version of Python.
Different behaviors between MATLAB variables and Python objects.
How to find help for Python functions.
Understanding Python and MATLAB import Commands
How to use Python
import
statements and MATLAB
import
commands.
Call User-Defined Python Module
Create a Python module used by examples in this documentation.
Reload Modified User-Defined Python Module
This example shows how to reload a modified Python module.
Install Supported Python Implementation
How to install a supported version of Python from
the www.python.org
website.
Python features not supported in MATLAB.
Undefined variable "py" or function "py.command"
Troubleshooting failures loading Python.
MATLAB types that are not supported in Python.
Python types that do not have compatible MATLAB types.
Troubleshooting Multidimensional Array Argument Errors
Error might be caused by input array with more than one non-singleton dimension.
Limitations to Indexing into Python Objects
Indexing features not supported in MATLAB.
MATLAB catches exceptions thrown by Python and
converts them into a matlab.exception.PyException
object.
Troubleshooting Error Messages
Tips to determine if an error originates in Python or MATLAB code.