pyenv - Change default environment of Python interpreter - MATLAB (original) (raw)

Change default environment of Python interpreter

Syntax

Description

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

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:

MATLAB accesses these settings when loading the Python interpreter:

pyenv displays details about the current (default) Python environment.

example

pyenv([Name,Value](#namevaluepairarguments)) specifies parameters for setting the Python environment. For example, pyenv(Version="3.10") changes the default Python version on Microsoft® Windows platforms to 3.10.

example

pe = pyenv(___) additionally returns environment details as a PythonEnvironment object. Use this syntax with any of the arguments in previous syntaxes.

Examples

collapse all

Display Python Version for Your System

Create list Variable Out-of-Process

Set the execution mode to OutOfProcess.

pyenv(ExecutionMode="OutOfProcess")

ans =

PythonEnvironment with properties:

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

Create the variable.

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

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

ans =

PythonEnvironment with properties:

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

Use Python Version 3.10

pe = pyenv; if pe.Status == "Loaded" && pe.Version ~= "3.10" disp('To change the Python version, restart MATLAB, then call pyenv(Version="3.10").') else pyenv(Version="3.10"); end

Verify Python Is Installed

To verify if Python is installed on your system, check the PythonEnvironment Version property.

pe = pyenv; if pe.Version == "" disp "Python not installed" end

Input Arguments

collapse all

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: pe = pyenv(Version="/usr/bin/python",ExecutionMode="OutOfProcess")

Version — Python version or executable

string | character vector

Python version number (Windows platforms only) or executable file name, specified as a string or a character vector. For information about supported versions, see Configure Your System to Use Python.

Example: pyenv(Version="3.10")

Example: pyenv(Version="/usr/bin/python")

Data Types: char | string

ExecutionMode — Execution mode

'InProcess' (default) | 'OutOfProcess'

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. Use this mode for performance-critical use cases.

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

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

Data Types: char | string

Version History

Introduced in R2019b