Main Content

Call User-Defined Python Module

This example shows how to call methods from the following Python® module. This module is used by examples in the documentation. The example explains how to create the module in MATLAB®. If you create mymod.py in a Python editor, be sure that the module is on the Python search path. This example also explains how to get help for calling the function, if you are not an experienced Python user.

  • Change your current folder to a writable folder.

  • Open a new file in MATLAB Editor.

  • Copy these commands and save the file as mymod.py.

    # mymod.py
    """Python module demonstrates passing MATLAB types to Python functions"""
    def search(words):
        """Return list of words containing 'son'"""
        newlist = [w for w in words if 'son' in w]
        return newlist
    
    def theend(words):
        """Append 'The End' to list of words"""
        words.append('The End')
        return words
    
  • From the MATLAB command prompt, add the current folder to the Python search path.

    if count(py.sys.path,pwd) == 0
        insert(py.sys.path,int32(0),pwd);
    end
  • To learn how to call the function, read the function signature for the search function in the mymod.py source file. The function takes one input argument, words.

    def search(words):
  • Read the function help in the mymod.py source file. According to the Python website documentation, help is in “a string literal that occurs as the first statement in a module, function, class, or method definition.” The help for search is:

    """Return list of words containing 'son'"""

    The function returns a list.

  • Create an input argument, a list of names, in MATLAB.

    N = py.list({'Jones','Johnson','James'})
    N = 
    
      Python list with no properties.
    
        ['Jones', 'Johnson', 'James']
  • Call the search function. Type py. in front of the module name and function name.

    names = py.mymod.search(N)
    names = 
    
      Python list with no properties.
    
        ['Johnson']

    The function returns a py.list value.

  • The original input N is unchanged.

    N
    N = 
    
      Python list with no properties.
    
        ['Jones', 'Johnson', 'James']

Reload Modified User-Defined Python Module

This example shows how to reload a modified Python module while running the Python interpreter in-process. For an alternative, see Reload Out-of-Process Python Interpreter.

Create Python Module

Change your current folder to a writable folder. Open a new file in MATLAB Editor.

Copy these statements defining a myfunc function and save the file as newmod.py.

# newmod.py
def myfunc():
    """Display message."""
    return 'version 1'

Call myfunc.

py.newmod.myfunc
ans = 

  Python str with no properties.

    version 1

Modify Module

Modify the function, replacing the return statement with the following:

    return 'version 2'

Save the file.

Unload Module

clear classes

MATLAB deletes all variables, scripts, and classes in the workspace. Best practice is to call clear from the command prompt. If you run clear classes in a workspace, such as a function workspace, clear might not be able to clear all the variables. Try running the command from the base workspace.

Import Modified Module

mod = py.importlib.import_module('newmod');

Reload Module in Python

py.importlib.reload(mod);

Call Function in Updated Module

Call the updated myfunc function.

py.newmod.myfunc
ans = 

  Python str with no properties.

    version 2

See Also

Related Topics