Main Content

Install and Configure Python for Use in MATLAB

R2026b

To 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-shared option. 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:

  1. 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.

  2. Search for the text MATLAB Support for Python.

  3. Select the add-on for the Python version you want to install.

  4. 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:

  1. Go to https://www.python.org/downloads/ and scroll to the Looking for a specific release section.

  2. Find the version you want and click Download. For supported version information, see Versions of Python Compatible with MATLAB Products by Release.

  3. 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-shared option. 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.

  1. 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 Python option from the language list at the upper left of the panel.

  2. 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: InProcess

Use 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 by pyenv, enter e = 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 can also set the variable using setenv. However, this setting applies only to the current MATLAB session.

  • To delete PYTHONHOME for your current MATLAB session, enter unsetenv("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 InProcess mode. 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 use OutOfProcess mode 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.myfunc, where myfunc is the function name, then you might have an installation problem. For troubleshooting help, see Unable to Resolve the Name py.myfunc.

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 by the PYTHONHOME environment variable, then clear the environment variable before calling pyenv. To clear the environment variable, at the MATLAB command prompt, enter unsetenv("PYTHONHOME"). The value set by pyenv persists across MATLAB sessions. For more information, see Set Python Version on Windows or Set Python Version on Mac and Linux.

  • PYTHONHOME environment variable, which specifies where to find the standard Python libraries. MATLAB uses this variable only when the value of pyenv.Status is NotLoaded. To view the value of the variable in MATLAB, enter getenv("PYTHONHOME"). For more information, see Use PYTHONHOME Environment Variable.

  • System PATH environment variable. To view the system path in MATLAB, enter getenv("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 InProcess mode, first restart MATLAB. Then, use the External Languages panel or pyenv to change the Python environment.

  • If Python is loaded in OutOfProcess mode, first terminate the Python process by calling terminate(pyenv). Then, use the External Languages panel or pyenv to 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.

See Also

Functions

Topics

External Websites