Main Content

PythonEnvironment

Python environment information

Description

PythonEnvironment objects contain information about the settings and status of the Python® interpreter. MATLAB® communicates with the interpreter when you call a py. command. Python environment information is persistent across different MATLAB sessions.

Creation

To create a PythonEnvironment object or change the settings, call pyenv.

MATLAB selects and loads a Python interpreter when you type a Python expression from MATLAB using the py namespace, for example, py.list. To change the interpreter:

  • If Python is loaded in InProcess ExecutionMode, then restart MATLAB and call pyenv with the new version information or execution mode.

  • If Python is loaded in OutOfProcess mode, then terminate the process associated with the Python interpreter by calling terminate before calling pyenv with the new version information or execution mode.

Properties

expand all

This property is read-only.

Python version, returned as a string. To set the Version property, call pyenv with the Version name-value argument set to a version number that MATLAB supports. For information about supported versions, see Configure Your System to Use Python.

Example: "3.10"

This property is read-only.

Name of the Python executable file, returned as a string. To set the Executable property, call pyenv with the Version name-value argument set to a relative or absolute path to the Python executable file.

Example: "/usr/local/bin/python"

This property is read-only.

Shared library file, returned as a string.

Example: "libpython3.10.so.1.0"

This property is read-only.

Home folder, returned as a string.

Example: "/usr/local/bin/../.."

Process status, specified as NotLoaded, Loaded, or Terminated.

Example: Terminated

Execution mode indicating whether to run Python scripts in the same process as MATLAB, specified as InProcess or OutOfProcess. To set the ExecutionMode property, call pyenv with the ExecutionMode name-value argument.

Example: OutOfProcess

This property is read-only.

Process ID that the Python interpreter is running on, returned as a string. If ExecutionMode is InProcess, then ProcessID is the MATLAB process ID. If Status is NotLoaded, then ProcessID is empty.

Example: "9388"

This property is read-only.

Process name in registry, returned as a string.

Example: "MATLABPyHost"

Object Functions

terminateTerminate process associated with Python interpreter

Examples

collapse all

pe = pyenv
pe = 

  PythonEnvironment with properties:

          Version: "3.10"
       Executable: "C:\Python310\pythonw.exe"
          Library: "C:\windows\system32\python310.dll"
             Home: "C:\Python310"
           Status: NotLoaded
    ExecutionMode: OutOfProcess
  

Limitations

  • Saving (serializing) PythonEnvironment objects to a MAT file is not supported.

Version History

Introduced in R2019b