Main Content

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 3.9 and 3.10. If your interpreter is already loaded in-process, then restart MATLAB.

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

          Version: "3.9"
       Executable: "C:\Python39\pythonw.exe"
          Library: "C:\WINDOWS\system32\python39.dll"
             Home: "C:\Python39"
           Status: Loaded
    ExecutionMode: OutOfProcess
        ProcessID: "15176"
      ProcessName: "MATLABPyHost"

Reload the Python version 3.10 interpreter.

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

          Version: "3.10"
       Executable: "C:\Python310\pythonw.exe"
          Library: "C:\WINDOWS\system32\python310.dll"
             Home: "C:\Python310"
           Status: Loaded
    ExecutionMode: OutOfProcess
        ProcessID: "24840"
      ProcessName: "MATLABPyHost"

Related Topics