pyenv

Change default environment of Python interpreter

Description

Use pyenv to change the default version or execution mode of the Python® interpreter. These changes are persistent across different MATLAB® sessions.

example

pe = pyenv returns details about the current (default) Python environment as a PythonEnvironment object.

example

pe = pyenv('Version',version) changes the default Python version on Microsoft® Windows® platforms.

Note

MATLAB automatically loads Python when you type py.command. You cannot change the interpreter after MATLAB loads Python. To change the interpreter, restart MATLAB, and then call pyenv.

pe = pyenv('Version',executable) specifies the full path to the Python executable. You can use this syntax on any platform or for repackaged CPython implementation downloads.

example

pe = pyenv('ExecutionMode',executionMode) changes the default execution mode of the Python interpreter.

pe = pyenv('Version',executable,'ExecutionMode',executionMode) changes the default version and execution mode of the interpreter.

Examples

collapse all

pe = pyenv;
pe.Version
ans = 

    "2.7"

Set the execution mode to OutOfProcess.

pyenv("ExecutionMode","OutOfProcess")
ans = 
  PythonEnvironment with properties:

          Version: "2.7"
       Executable: "C:\Python27\pythonw.exe"
          Library: "C:\windows\system32\python27.dll"
             Home: "C:\Python27"
           Status: NotLoaded
    ExecutionMode: OutOfProcess

Create the variable.

py.list({'Monday','Tuesday','Wednesday','Thursday','Friday'});

Show the process. MATLAB displays information specific to your environment.

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: "8196"
      ProcessName: "MATLABPyHost"
pe = pyenv;
if pe.Status == 'Loaded'
    disp('To change the Python version, restart MATLAB, then call pyenv('Version','2.7').')
else
    pyenv('Version','2.7');
end

Input Arguments

collapse all

Python version number, specified as a string or character vector (Windows platform only). The version must contain the major and minor version numbers separated by a period. For information about supported versions, see Configure Your System to Use Python.

pyenv looks for the version in the Windows registry. If you download the Python application from www.python.org/downloads, the installation automatically adds the version to the registry. If you download the application from a different source, you must either add it to the registry or use the pyenv(executable) syntax to change the version.

Example: 2.7

Data Types: char | string

Name of an existing Python executable file, specified as a string or character vector. This argument must contain the name of the Python executable file, and it can contain the full path.

Example: /usr/bin/python

Data Types: char | string

Execution mode indicating whether to run Python scripts in the same process as MATLAB, specified as 'InProcess' or 'OutOfProcess'. The default 'InProcess' runs the scripts in the MATLAB process and is suggested for performance-critical use cases.

'OutOfProcess' starts a separate process and is used for safe execution of Python scripts and libraries. Select 'OutOfProcess' for:

  • Working with Python libraries which require a different version of a 3rd party library also required by MATLAB

  • Debugging workflows

When you call a Python function out-of-process, there is overhead associated with the call. This behavior might affect performance.

Introduced in R2019b