Reload Out-of-Process Python Interpreter

When you run the Python® interpreter out-of-process, you can terminate the interpreter and start a new one, possibly with different version settings, without restarting MATLAB®.

To reload an in-process Python interpreter, see the example Reload Modified User-Defined Python Module.

This example assumes that you have Python version 2.7 and 3.8. If your interpreter is already loaded in-process, then restart MATLAB.

pe = pyenv;
if pe.Status == 'NotLoaded'
    pyenv("ExecutionMode","OutOfProcess","Version","3.8");
end
py.list; % Call a Python function to load interpreter
pyenv
ans = 
  PythonEnvironment with properties:

          Version: "3.8"
       Executable: "C:\Python36\pythonw.exe"
          Library: "C:\WINDOWS\system32\python38.dll"
             Home: "C:\Python38"
           Status: Loaded
    ExecutionMode: OutOfProcess
        ProcessID: "15176"
      ProcessName: "MATLABPyHost"

Reload the Python version 2.7 interpreter.

terminate(pyenv)
pyenv("Version","2.7");
py.list; % Reload interpreter
pyenv
ans = 
  PythonEnvironment with properties:

          Version: "2.7"
       Executable: "C:\Python27\pythonw.exe"
          Library: "C:\WINDOWS\system32\python27.dll"
             Home: "C:\Python27"
           Status: Loaded
    ExecutionMode: OutOfProcess
        ProcessID: "24840"
      ProcessName: "MATLABPyHost"

Related Topics