Call Python from MATLAB
You can access Python libraries directly from MATLAB by adding the py. prefix to the
Python name. See Access Python Modules from MATLAB. For
example:
py.list({'This','is a','list'}) % Call built-in function list
py.textwrap.wrap('This is a string') % Call wrap function in module textwrappyrun
or pyrunfile functions. For
example:pyrun("l = ['A','new','list']") % Call list in Python interpreterIf instead you want to call MATLAB functions from Python applications, see Call MATLAB from Python for more information.
Functions
Live Editor Tasks
| Run Python Code | Run Python statements or script files in the Live Editor (Since R2024a) |
Topics
Use Python Environments in MATLAB
- Manage Python Environments Using External Languages Panel
You can use the Python view in the External Languages panel to configure and maintain your Python environments directly from MATLAB.
Use Python Libraries in MATLAB
- Access Python Modules from MATLAB
Create and use a Python object in MATLAB. - Configure Your System to Use Python
Verify that you have installed a supported version of Python. - Call User-Defined Python Module
Create a Python module used by examples in this documentation. - Understand Python Function Arguments
Python method syntax differs from MATLAB method syntax. - Advanced Topics
Learn about code pattern differences between MATLAB and Python. - Out-of-Process Execution of Python Functionality
Execute Python scripts in processes that are separate from the MATLAB process. - Reload Out-of-Process Python Interpreter
Reload out-of-process Python interpreter without restarting MATLAB.
Run Python Code from MATLAB
- Directly Call Python Functionality from MATLAB
Call functionality from Python libraries or execute Python statements directly from MATLAB.
Passing Data
- Pass Data Between MATLAB and Python from MATLAB
The Python interface converts MATLAB data into compatible Python data types. - Access Elements in Python Container Types
A Python container is typically a sequence type (listortuple) or a mapping type (dict). - Use Python Datetime Types in MATLAB
MATLAB supports conversions between MATLABdatetimevalues and Pythondatetimeor NumPydatetime64values. - Use Python Duration Types in MATLAB
MATLAB supports conversions between MATLABdurationvalues and Pythontimedeltaor NumPytimedelta64values. - Pass Python Function to Python map Function
This example shows how to display the length of each word in a list.
Troubleshooting
Determine If Error is Python or MATLAB Error
Determine if an error originates in Python or MATLAB code.
Unable to resolve the name py.myfunc
Troubleshoot failures loading Python.
Some Python features are not currently supported in MATLAB.
MATLAB catches exceptions thrown by Python and converts them into a matlab.exception.PyException object.
Troubleshooting Matrix and Numeric Argument Errors
Errors might be caused by an input array with more than one dimension whose size is greater than 1.
Error Converting Elements of list or tuple
Use string and numeric converters for list and
tuple types.
Featured Examples
Teaching Resources
Programming: A Starter Project Using MATLAB with Python
Learn how to call Python code, convert data types and units, and use App Designer to build an app to display your results.

