Configure Your System to Use Python
Python Support
To call Python® modules in MATLAB®, you must have a supported version of the reference implementation (CPython) installed on your system. Install a distribution, such as those found at https://www.python.org/downloads/. MATLAB does not support CPython versions installed from the Microsoft® store. For supported version information, see Versions of Python Compatible with MATLAB Products by Release.
If you are on Windows®, you need to install a distribution, if you have not already done so. For more information, see Install Supported Python Implementation.
If you are on a Linux® platform, you might already have Python installed. Call
pyenv
to verify that you have a supported version. For download information, see Install Supported Python Implementation.If you are on a Mac platform, verify that the MATLAB and Python builds are consistent with your architecture (Apple silicon or Intel® processor). For more information, see How to use Python from MATLAB on Mac with Apple Silicon.
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:
If Python is loaded in
InProcess
ExecutionMode
, then restart MATLAB and runpyenv
with the new version information.If Python is loaded in
OutOfProcess
mode, restart MATLAB and runpyenv
with the new version information.
MATLAB accesses these settings when loading the Python interpreter:
User settings for the Python environment, configured using the
pyenv
function. If the version you specify conflicts with the version specified byPYTHONHOME
, then delete the environment variable before callingpyenv
. The value set bypyenv
is persistent across MATLAB sessions. For more information, see Set Python Version on Windows Platform or Set Python Version on Mac and Linux Platforms.PYTHONHOME
environment variable, specifies where to find the standard Python libraries if the value ofpyenv.Status
isNotLoaded
. To view the value of the variable in MATLAB, typegetenv("PYTHONHOME")
. For more information, see Use PYTHONHOME Environment Variable.System
PATH
environment variable. To view the system path in MATLAB, typegetenv("PATH")
.Windows registry.
To verify that Python is installed on your system, open the Python interpreter from your system prompt and call Python functions.
To determine which version MATLAB is using, call pyenv
. For example, on Windows:
pe = pyenv
pe = PythonEnvironment with properties: Version: "3.10" Executable: "C:\Users\username\AppData\Local\Programs\Python\Python310\pythonw.exe" Library: "C:\Users\username\AppData\Local\Programs\Python\Python310\python310.dll" Home: "C:\Users\username\AppData\Local\Programs\Python\Python310" Status: NotLoaded ExecutionMode: InProcess
On Linux:
pe = pyenv
pe = PythonEnvironment with properties: Version: "3.9" Executable: "/usr/bin/python3" Library: "libpython3.9.so.1.0" Home: "/usr" Status: NotLoaded ExecutionMode: InProcess
Install Supported Python Implementation
Access https://www.python.org/downloads/ and scroll to the Looking for a specific release section.
Find the version you want and click Download. For supported version information, see Versions of Python Compatible with MATLAB Products by Release.
Click the format you want for the 64-bit version and follow the online instructions.
If you get the error message Unable to resolve the name py.myfunc, you might have an installation problem.
Install Python in Virtual Environment
You can create a virtual environment for a specific version of Python using the Python
venv
module. You can also install the MATLAB Engine API for Python in a virtual environment. For venv
instructions, see the
MATLAB
Answers™ article Python virtual environments with Python Interface. For information about virtual
Python environments, see the Python tutorial Virtual Environments and Packages.
Set Python Version on Windows Platform
On Windows platforms, use either:
pyenv(Version="version")
or
pyenv(Version="executable")
where executable
is the full path to the Python executable file. For more information, see the Version
name-value argument for pyenv
. For example, type:
pyenv(Version="3.11");
Note
If you downloaded a Python interpreter, but did not register it in the Windows registry, use:
pyenv(Version="executable")
For example, type:
pyenv(Version="C:\Users\username\AppData\Local\Programs\Python\Python311\python.exe");
Download 64-Bit Version of Python on Windows Platforms
The architecture of Python must match the architecture of MATLAB. For more information, see Install Supported Python Implementation.
Set Python Version on Mac and Linux Platforms
To set the version, type:
pyenv(Version="executable")
where executable
is the full path to the Python executable file. For more information, see the Version
name-value argument for pyenv
. For example, type:
pyenv(Version="/usr/bin/python3.11")
Requirements for Building Python Executable
On Linux and Mac systems, if you build the Python executable, configure the build with the --enable-shared
option.
Use PYTHONHOME
Environment Variable
When possible, use pyenv
to set the Python interpreter. In cases where pyenv
is not usable, for
example, when running MATLAB apps, an alternative way to set the interpreter is to use the
PYTHONHOME
environment variable. If you set the version with
pyenv
and PYTHONHOME
, then the versions must be
the same.
To view the variable in MATLAB, type
getenv("PYTHONHOME")
. To view the version set bypyenv
, typee = pyenv; e.Version
To set
PYTHONHOME
, see https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHOME. For information about setting environment variables, refer to your operating system documentation. You also can set the variable usingsetenv
. However, this setting applies only to the current MATLAB session.To delete
PYTHONHOME
for your current MATLAB session, typeunsetenv("PYTHONHOME")
.
You cannot change the Python version if you have already loaded the interpreter. MATLAB loads a Python interpreter when you type a Python statement from MATLAB using the py
namespace, for example,
py.sys.path
. To change the interpreter:
If Python is loaded in
InProcess
ExecutionMode
in a single MATLAB session, then restart MATLAB and runpyenv
with the new version information.If Python is loaded in
OutOfProcess
mode, then callterminate
and runpyenv
with the new version information.