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.

Examples

collapse all

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"

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

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

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

Name-Value Arguments

collapse all

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.

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

Python version number (Windows platforms only) or executable filename, specified as a string scalar or 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")

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