Main Content

PythonEnvironment

Python environment information

Since R2019b

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. The Python interpreter is loaded when you call a Python command in the py package. You cannot change the interpreter after MATLAB loads Python. To change the interpreter, restart MATLAB, and then call pyenv.

Properties

expand all

This property is read-only.

Python version, returned as a string. To set the Version property, call pyenv with the version 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 argument set to the name of the Python executable file and, optionally, the full path.

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 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 not Loaded, 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 into a MAT-file is not supported.

Version History

Introduced in R2019b