Configure Your System to Use Python

Python Version Support

To call Python® modules in MATLAB®, you must have a supported version of the reference implementation (CPython) installed on your system. MATLAB supports versions 2.7, 3.6, 3.7, and 3.8. For more information, see Versions of Python Supported by MATLAB Products by Release. If you are on a Linux® or Mac platform, you already have Python installed. If you are on Windows®, you need to install a distribution, such as those found at https://www.python.org/download, if you have not already done so. For more information, see Install Supported Python Implementation.

To verify that Python is installed on your system, open the Python interpreter from your system prompt and call Python functions.

By default, MATLAB selects the version of Python based on your system path. To view the system path in MATLAB, use the getenv('path') command. To determine which version MATLAB is using, call the pyenv function.

pe = pyenv;
pe.Version
ans = 

    "3.6"

The value set by pyenv is persistent across MATLAB sessions. If you have multiple supported versions, use pyenv to display the version currently used by MATLAB. MATLAB automatically selects and loads a Python version when you type a Python command. For example, to call funcname, type:

py.funcname

To change versions:

  • If Python is loaded in InProcess ExecutionMode in a single MATLAB session, then restart MATLAB and run pyenv with the new version information.

  • If Python is loaded in OutOfProcess mode, then call terminate and run pyenv with the new version information.

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. MATLAB supports versions 2.7, 3.6, 3.7, and 3.8.

  • Click the format you want for the 64-bit version and follow the online instructions.

    Note

    To install version 2.7 for 64-bit MATLAB on Microsoft® Windows systems, select the 64-bit Python version, called Windows x86-64 MSI installer.

If you get the error message Unable to resolve the name py.myfunc, you might have an installation problem.

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.

Note

If you downloaded a Python interpreter, but did not register it in the Windows registry, use:

pyenv('Version','executable')

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.

Requirements for Building Python Executable

On Linux and Mac systems, if you build the Python executable, configure the build with the --enable-shared option.

See Also

Related Topics

External Websites