Main Content

Run Python Code

Run Python statements or script files in the Live Editor

Since R2024a

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.

Run Python statements or script files in the Live Editor

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 or run. Select Run Python Code from the suggested command completions.

Examples

expand all

Interactively run Python code using the Run Python Code task in the Live Editor. In this example, the Python code calculates the length of the hypotenuse, h, of a right triangle using the two other sides, a and b, of the triangle.

Create MATLAB variables a and b.

a = 3;
b = 4;

Open the Run Python Code task in the Live Editor, and select Code as the input type. Type the following Python code in the Enter Python code section. The Python math.sqrt function uses the MATLAB variables a and b as inputs. In the Output options section, select Return all to return all the generated Python variables to the MATLAB workspace, and select the Display outputs check box to display the values of the variables as task output. Then click the Run button.

import math

h = math.sqrt(a**2 + b**2)

Run Python Code Live Editor task with parameters described in the example

Run Python code in a Python file in MATLAB, and return some of the generated Python variables to the MATLAB workspace.

Create a Python file named circumference.py from the following statements. This Python code calculates the circumference, c, of a circle using the radius, r.

import math 
r = 5
c = 2 * math.pi * r

Open the Run Python Code task in the Live Editor, and select File as the input type. Then, input the path to circumference.py in the Select Python file section.

By default, the Live Editor task returns all the generated Python variables to the MATLAB workspace. Return only c by selecting Return selected variables and moving c from the Generated Python variables list to the Output list. Select the Display outputs check box to see the value of c in MATLAB. Then click the Run button.

Run Python Code Live Editor task with parameters described in the example

Run Python code in a Python file in MATLAB with command line arguments.

Create a Python file named calculator.py from the following statements. This Python code takes two integer inputs, x and y, from the user on the command line. The code subtracts x and y and stores the difference in diff and then multiplies x and y and stores the product in prod.

import sys

arg_list = list()

for arg in sys.argv:
  arg_list.append(arg)

if len(sys.argv) < 2:
  print("Initialize x and y using integers as command line arguments.")
  exit()

x = int(arg_list[1])
y = int(arg_list[2])

diff = x - y
prod = x * y

Open the Run Python Code task in the Live Editor, and select File as the input type. Then, input the path to calculator.py in the Select Python file section.

In the Specify command line arguments section, specify command line arguments 5 and 6 by using the + button to add arguments. The argument specified at the top of the list is arg_list[1], or in this case, 5, and the argument next in the list is arg_list[2], or in this case, 6. Select the Display outputs check box to see the values of the Python variables in the MATLAB workspace. Then click the Run button.

Run Python Code Live Editor task with parameters described in the example

Related Examples

Parameters

expand all

This task operates on the specified type of Python code:

  • Code ― Type Python statements in the text box in the Enter Python code section.

  • File ― Specify the path to a Python file in the Select Python file section.

You can pass a MATLAB workspace variable to Python code in the task using these methods:

  • Assign the MATLAB variable to a Python variable in the task.

  • Pass the MATLAB variable to a Python function in the task.

  • Use the MATLAB variable as an operand in a Python operation in the task.

If you select Code as the input type and create a Python variable in the task, the Python variable takes precedence over a MATLAB variable with the same name created later in the code. In other words, the Run Python Code task does not use the variable in the MATLAB workspace.

If you select the input type as File, you can specify command line arguments. These arguments are useful for passing inputs to the specified Python file. Type one argument in each Argument box. Click the + button to add more command line arguments and the button to remove command line arguments from the list. You can reorder the arguments using the up and down triangle buttons.

Specify which generated Python variables you want the Live Editor task to return to the MATLAB workspace:

  • Return all ― Return all the variables that MATLAB detects in the Python code to the MATLAB workspace.

  • Return selected variables ― Return only the variables in the Output list. Move variables to the Output list by selecting them in the Generated Python variables list and then clicking the right-arrow button.

This option controls whether to display the selected Python variables as task output.

Limitations

  • If you make a change to a Python class definition after the task has run, restart the Python interpreter or reload the Python module. For more information about reloading modified user-defined Python modules, see Call User-Defined Python Module.

Version History

Introduced in R2024a

See Also

Functions