Run Python Code
Description
The Run Python Code task lets you interactively run Python® code or files. The task automatically generates MATLAB® code for your live script. For more information about Live Editor tasks generally, see Add Interactive Tasks to a Live Script.
Using this task, you can:
Write and execute Python code.
Execute code in Python files.
Return Python variables to the MATLAB workspace.
Pass MATLAB data to the Python code.
Specify command-line arguments.
Open the Task
To add the Run Python Code task to a live script in the MATLAB Editor:
On the Live Editor tab, select Task > Run Python Code.
In a code block in the script, type a relevant keyword, such as
Python
orrun
. SelectRun Python Code
from the suggested command completions.
Parameters
Limitations
Python classes defined using
pyrun
orpyrunfile
cannot be modified if you return an instance of the class to MATLAB. If you need to change class definitions, restart the interpreter session:terminate(pyenv) pyenv(ExecutionMode="OutOfProcess")
Alternatively, restart MATLAB for
"InProcess"
.The
pyrun
andpyrunfile
functions do not support classes with local variables that are initialized by other local variables through methods. For such usage, create a module and access it using thepy.
prefix.Type conversion based on Python type hints is not supported for
pyrun
,pyrunfile
or the Run Python Code task where native Python code is provided as input.
Version History
Introduced in R2024a