Main Content
pyenv
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.
Note
MATLAB automatically loads Python when you type py.
. You cannot change the interpreter after MATLAB loads Python. To change the interpreter, restart MATLAB, and then call command
pyenv
.
example
pyenv
displays details about the current [default] Python environment.
example
pyenv[
specifies parameters for setting the Python
environment. For example, Name,Value
]pyenv['Version','2.7']
changes the default Python version on Microsoft® Windows® platforms to 2.7.
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: "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.
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"
Use Python Version 2.7
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
Test 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 as Name1=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.
If
Version
specifies a number, then the value must contain the major and minor version numbers separated by a period. The function looks for the version in the Windows registry. If you download the Python application fromwww.python.org/downloads
, then the installation automatically adds the version to the registry. If you download the application from a different source, you must either add the version to the registry or callpyenv
with the executable file name to change the version.If
Version
is the name of an existing executable Python file, then the value must contain the name of the file and the full or relative path to the file. You can use this syntax on any platform or for repackaged CPython implementation downloads.
Example: '2.7'
Example: '/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:
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.
Data Types: char
| string
Version History
Introduced in R2019b