Main Content

Python Libraries in MATLAB

Directly call Python® functionality from MATLAB®

You can access Python libraries directly from MATLAB by adding the py. prefix to the Python name. To call content in the Python standard library, add py. in front of the Python function or class name. To call content in available modules, add py. in front of the Python module name followed by the Python function or class name. For example:

py.list({'This','is a','list'})      % Calls built-in function list
py.textwrap.wrap('This is a string') % Calls wrap function in module textwrap

You do not need to import modules in order to use them. However, you may import Python names into your MATLAB function in the same way that you can import content in MATLAB packages. For more information, see Understanding Python and MATLAB import Commands.

If instead you want to call MATLAB functions from Python applications, see 从 Python 调用 MATLAB for more information.



pyenv更改 Python 解释器的默认环境
PythonEnvironmentPython 环境信息
pyargsPython 函数创建关键字参数
matlab.exception.PyException捕获 Python 异常的错误信息


Using Python Libraries

Access Python Modules from MATLAB - Getting Started

How to create and use a Python object in MATLAB.


如何验证您已安装受支持的 Python 版本。

调用用户定义的 Python 模块

创建本文档中的示例使用的一个 Python 模块。

Understand Python Function Arguments

Python method syntax which might be unfamiliar to MATLAB users.

Advanced Topics

Code pattern differences you should be aware of.

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.

Passing Data

将数据传递给 Python

当调用 Python 函数时,MATLAB 将 MATLAB 数据转换为最适合在 Python 语言中表达该数据的类型。

Access Elements in Python Container Types

A Python container is typically a sequence type (list or tuple) or a mapping type (dict).

Pass Python Function to Python map Function

This example shows how to display the length of each word in a list.


Determine if Error is Python or MATLAB Error

Tips to determine if an error originates in Python or MATLAB code.

Limitations to Python Support

Python features not supported in MATLAB.

Handle Python Exceptions

MATLAB catches exceptions thrown by Python and converts them into a matlab.exception.PyException object.

Troubleshooting Matrix and Numeric Argument Errors

Error might be caused by input array with more than one non-singleton dimension.