Change default environment of Python interpreter
pyenv to change the default version or execution mode of
the Python® interpreter. These changes are persistent across different MATLAB® sessions.
pe = pyenv('Version', changes the
default Python version on Microsoft®
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
specifies the full path to the Python executable. You can use this syntax on any platform or for repackaged
CPython implementation downloads.
pe = pyenv('Version',
pe = pyenv; pe.Version
ans = "2.7"
Set the execution mode to
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.
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"
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
To verify if Python is installed on your system, check the
pe = pyenv; if isempty(pe.Version) disp "Python not installed" end
version— Python version
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
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.
executable— Name of Python executable file
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.
executionMode— Execution mode
Execution mode indicating whether to run Python scripts in the same process as MATLAB, specified as
'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
Working with Python libraries which require a different version of a 3rd party library also required by MATLAB
When you call a Python function out-of-process, there is overhead associated with the call. This behavior might affect performance.