Main Content


Run Python script file from MATLAB

Since R2021b



    pyrunfile(file) executes the Python® statements in the file.

    Unlike the pyrun function, variables created in the Python workspace using the pyrunfile function are not persistent. Subsequent calls to pyrunfile do not have access to the variables.


    pyrunfile(file input) executes the Python statements with input arguments. Python scripts read command-line arguments as strings.


    outvars = pyrunfile(file,outputs) assigns lhs variables. The functions will also allow users to select MATLAB® workspace variables to be passed as input to the given Python code, and return all or a user-selected subset of Python variables that are processed in the script, back to MATLAB.


    outvars = pyrunfile(file,outputs,pyName=pyValue) executes the code with one or more name-value pair arguments.


    collapse all

    Call Python statements in file

    Create Python script from these statements:

    greeting = "hello"

    Display the output to the MATLAB command line.


    Create a Python script and pass a string.

    Create from these statements:

    import sys
    greeting = sys.argv[1]

    Pass a string to the script and display the output.

    pyrunfile(" 'hello world'")
    hello world

    Run a Python script from MATLAB and return a variable generated by the script to MATLAB.

    Create Python script from this statement:

    l = ['A', 'new', 'list']

    Run the script to create the list and return it to MATLAB in variable data.

    data = pyrunfile("", "l")
    data = 
      Python list with no properties.
        ['A', 'new', 'list']

    Call a Python script that takes input arguments.

    Create Python script from these statements. The script takes input arguments x and y and returns variable z.

    def add(a,c):
        b = a+c
        return b
    z = add(x,y)

    Pass values for x and y. Return the variable z in the MATLAB variable res.

    res = pyrunfile("","z",x=3,y=2)
    res = 5

    Input Arguments

    collapse all

    Python file containing Python expressions or statements, specified as string scalar or character vector.

    Example: ""

    Python file containing Python expressions or statements with command line input arguments, specified as string scalar or character vector.

    Example: " var"

    One or more Python variable names, specified as a string array. Variables can be local or global. MATLAB assigns the output of code to each variable named by outputs and returns the values in outvars.

    Example: "L"

    One or more Input argument names and values to pass to the Python code, specified as keyword and value arguments. pyName is the Python name of a variable, and pyValue is the assigned value. You can specify several name and value pair arguments in any order as pyName1=pyValue1,...,pyNameN=pyValueN.

    Example: x=3

    Output Arguments

    collapse all

    One or more MATLAB workspace variable names, returned as valid types from code. The number of outvars corresponds to the number of outvars arguments. If you want to access Python data, then you must explicitly return Python objects to MATLAB using outvars.


    • Python classes defined using pyrun or pyrunfile cannot be modified if you return an instance of the class to MATLAB. If you need to change class definitions, restart the interpreter session:


      Alternatively, restart MATLAB for "InProcess".

      The pyrun and pyrunfile 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 the py. prefix.

    Version History

    Introduced in R2021b