Install and Configure Python for Use in MATLAB
R2026bTo call Python® from MATLAB®, you must have a supported version of the reference implementation (CPython) installed on your system. You must also configure your Python environment to work with MATLAB.
Check Existing Python Installation
If you already have Python installed, check whether MATLAB supports your Python version at Versions of Python Compatible with MATLAB Products by Release.
Your existing Python installation must also meet these platform-specific requirements:
Windows® — The 64-bit version of Python is installed. 32-bit versions are not supported.
Windows — Python was not installed from the Microsoft® Store.
Linux® and Mac — If you built Python from source, you configured the build with the
--enable-sharedoption. For details, see Configure Python in the Python documentation.Mac — Your MATLAB and Python installations have the same architecture (Apple silicon or Intel® processor). For more information, see How to use Python from MATLAB on Mac with Apple Silicon.
Install Python
If you do not have a supported version of Python installed on your system, you can install one either from the Add-Ons panel in
MATLAB or from python.org.
Install Python from MATLAB Add-Ons Panel
Since R2026b
To install Python from the Add-Ons panel in MATLAB, follow these steps:
Open the Add-Ons panel by clicking the Add-Ons icon on the left sidebar of the desktop. If the Add-Ons icon is not present, click the Open more panels button
and select the Add-Ons panel.Search for the text MATLAB Support for Python.
Select the add-on for the Python version you want to install.
Click the Install button.
You can now select this installed version of Python as your Python environment using pyenv or the
External Languages panel. For details, see Manage Python Environments Using External Languages Panel.
Install Python from python.org
To install Python from python.org, follow these steps:
Go to
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 to install and follow the online instructions.
On Windows, select the 64-bit format.
On Linux and Mac, if you build the Python executable from source, configure the build with the
--enable-sharedoption. For more details, see Configure Python in the Python documentation.
Configure MATLAB to Use Python Environment
You can configure MATLAB to use either a base environment (a system-wide Python installation) or virtual environment (an isolated, project-specific folder of Python packages). If you do not already have a virtual environment, you can create one from your base environment by using the External Languages panel (see Manage Python Environments Using External Languages Panel). Alternatively, to create a virtual environment programmatically, see the MATLAB article Python virtual environments with MATLAB and the Python tutorial Virtual Environments and Packages.
To set your Python environment, use the External Languages panel, the pyenv
function, or the PYTHONHOME environment variable.
Select Python Environment Using External Languages Panel
Since R2026a
The simplest way to configure MATLAB to use Python is to select a base or virtual environment by using the External Languages panel.
To open the Python view in the External Languages panel, click the Open more panels button
on any sidebar and click
External Languages. Then select the Pythonoption from the language list at the upper left of the panel.Select your preferred base or virtual Python environment by right-clicking an environment in the All Python Environments section and selecting Set as Selected Environment.
The selected environment appears in the Selected Python Environment section of the External Languages panel.
For more details, see Manage Python Environments Using External Languages Panel.
Set Python Version on Windows
If you installed Python using the standard installer, Python is registered in the Windows registry. Use the Version
name-value of pyenv to set which Python version to use. For example:
pyenv(Version="3.13");If you performed a nonstandard installation, such as copying Python files to your computer without running the installer, set the Python version using the path to the Python executable file for the base or virtual environment. For example:
pyenv(Version="C:\Users\username\AppData\Local\Programs\Python\Python313\python.exe");
pyenv returns a PythonEnvironment object
containing information about the current Python environment.
PythonEnvironment with properties:
Version: "3.13"
Executable: "C:\Users\username\AppData\Local\Programs\Python\Python313\python.exe"
Library: "C:\Users\username\AppData\Local\Programs\Python\Python313\python313.dll"
Home: "C:\Users\username\AppData\Local\Programs\Python\Python313"
Status: NotLoaded
ExecutionMode: InProcess
Set Python Version on Mac and Linux
To set which Python version to use on Mac and Linux, use the Version
name-value of pyenv to specify the full path to the Python executable file for the base or virtual environment. For example:
pyenv(Version="/usr/bin/python3.13")pyenv returns a PythonEnvironment object
containing information about the current Python environment.
PythonEnvironment with properties:
Version: "3.13"
Executable: "/usr/bin/python3"
Library: "libpython3.13.so.1.0"
Home: "/usr"
Status: NotLoaded
ExecutionMode: InProcessUse PYTHONHOME Environment Variable
When possible, use the External Languages panel or pyenv to set
the Python version. In cases where the panel or pyenv is not
usable, for example, when running MATLAB apps, an alternative way to set the version 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 value of the environment variable, at the MATLAB command prompt, enter
getenv("PYTHONHOME"). To view the version set bypyenv, entere = pyenv; e.VersionTo set
PYTHONHOME, seehttps://docs.python.org/3/using/cmdline.html#envvar-PYTHONHOME. For information about setting environment variables, refer to your operating system documentation. You can also set the variable usingsetenv. However, this setting applies only to the current MATLAB session.To delete
PYTHONHOMEfor your current MATLAB session, enterunsetenv("PYTHONHOME").
Change Execution Mode (Optional)
After you select the Python environment, MATLAB does not immediately load the Python interpreter. Loading happens the first time you call a Python function in MATLAB. Before that first call, you can optionally change the execution mode for
Python. You can execute Python either in the same process as MATLAB (InProcess mode) or in a separate process
(OutOfProcess mode).
By default, Python runs in
InProcessmode. This execution mode has performance benefits and is recommended for running existing Python code.If you are actively developing Python code, set the execution mode to
OutOfProcess. This mode protects your MATLAB session from crashes in unstable code and lets you interrupt long-running Python code with Ctrl+C. Also useOutOfProcessmode if you need to use third-party libraries that conflict with MATLAB.
To set the execution mode, from the External Languages panel, click the Settings button
and select OutOfProcess.
Alternatively, at the MATLAB command prompt, enter pyenv(ExecutionMode="OutOfProcess").
For more information, see Out-of-Process Execution of Python Functionality.
Confirm Python Works with MATLAB
To confirm that Python works with MATLAB, load the Python interpreter by entering a Python expression in MATLAB. For example:
py.list
ans =
Python list with values:
[]
Use string, double, datetime or cell function to convert to a MATLAB array.If you get the error message Unable to resolve the name
py., where
myfunc is the function name, then you
might have an installation problem. For troubleshooting help, see Unable to Resolve the Name py.myfunc.myfunc
MATLAB accesses these settings when loading the Python interpreter:
User settings for the Python environment, configured using the
pyenvfunction. If the version you specify conflicts with the version specified by thePYTHONHOMEenvironment variable, then clear the environment variable before callingpyenv. To clear the environment variable, at the MATLAB command prompt, enterunsetenv("PYTHONHOME"). The value set bypyenvpersists across MATLAB sessions. For more information, see Set Python Version on Windows or Set Python Version on Mac and Linux.PYTHONHOMEenvironment variable, which specifies where to find the standard Python libraries. MATLAB uses this variable only when the value ofpyenv.StatusisNotLoaded. To view the value of the variable in MATLAB, entergetenv("PYTHONHOME"). For more information, see Use PYTHONHOME Environment Variable.System
PATHenvironment variable. To view the system path in MATLAB, entergetenv("PATH").Windows registry.
Switch Python Environments
You can change the Python environment that MATLAB uses through the External Languages panel or by calling
pyenv. The process for changing Python environments depends on the execution mode.
If Python is loaded in
InProcessmode, first restart MATLAB. Then, use the External Languages panel orpyenvto change the Python environment.If Python is loaded in
OutOfProcessmode, first terminate the Python process by callingterminate(pyenv). Then, use the External Languages panel orpyenvto change the Python environment.
To remove an old Python environment from the External Languages panel, right-click the environment in the All Python Environments list and select Remove Environment. Removing an environment does not delete it from your system. You can add it back later. You cannot remove an environment installed from a MATLAB Support for Python add-on in this way. To remove that environment, uninstall the support package from the Add-Ons panel and then refresh the External Languages panel by using the Refresh button.