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 a Linux® or Mac platform, you already have Python installed. 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.
To verify that Python is installed on your system, open the Python interpreter from your system prompt and call Python functions. Make sure that the Python path is included in your system path environment variable.
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.8"
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 statement. 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 runpyenv
with the new version information.If Python is loaded in
OutOfProcess
mode, then callterminate
and runpyenv
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. 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 Python environment using the Python
venv
module, which contains a specific version of Python and MATLAB Engine API for Python. 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.
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.