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,'') == 0
        insert(py.sys.path,int32(0),'');
    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.

When you use this workflow, MATLAB deletes all variables, scripts, and classes in the workspace. For more information, see the clear classes function.

The Python calling syntax to reload the module depends on your Python version. To verify your Python version, use the MATLAB pyenv function.

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 mymod.py.

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

Call myfunc.

py.mymod.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.

Import Modified Module

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

Reload Module in Python Version 2.7

py.reload(mod);

Reload Module in Python Versions 3.x

py.importlib.reload(mod);

Call Function in Updated Module

Call the updated myfunc function.

py.mymod.myfunc
ans = 

  Python str with no properties.

    version 2

See Also

|

Related Topics