Import Python user modules in Matlab

This page shows how to setup Matlab with user Python install (e.g. Anaconda Python) on Linux, Mac or Windows. It goes without saying, the Python program must work first with Python alone for it to work in Matlab. For concurrent Python modules using asyncio, you may need to create a shim function to allow Matlab to call the Python module.

Install Python for Matlab

Matlab is designed to work with specific Python versions for each Matlab version. Matlab will not specifically tell you when you’re using an incompatible Python version, but you may get unstable operation or errors.

In general, we recommend for Python ↔ Matlab interfacing:

  • Matlab ≥ R2018b, as R2018b made exchanging arrays with Python much more efficient and easy.
  • Python 3.6 or 3.7 for significant new functionality and performance
Matlab versionPython version
R2019aup to 3.7
R2018bup to 3.6

You can just type in Matlab

pyversion

If you don’t see your desired Python version or install, here’s how to select a specific Python version.

[optional] select Python version

This section is optional, for example if Matlab pyversion doesn’t find the desired Python version.

Create and activate Python virtual environment (assuming Miniconda Python). This example is for Python 3.7.

conda create -n py37 python==3.7

conda activate py37

The exact location of the Python executable to put in Matlab pyversion() command is determined by:

python -c "import sys; print(sys.executable)"

This examples assumes a Python 3.7 executable is at ~/miniconda3/envs/py37/bin/python. Setup Matlab to use Python 3.7 by adding to ~/Documents/MATLAB/startup.m

pyversion('~/miniconda3/envs/py37/bin/python')

Verify Matlab → Python config by typing pyversion from within Matlab:

version: '3.7'
executable: '~/miniconda3/envs/py37/bin/python'
library: '~/miniconda3/envs/py37/lib/libpython3.7m.so'
  home: '~/miniconda3/envs/py37'
isloaded: 0

Notes

  • Python module import is implicit in the Matlab → Python module function call. There is no need to import numpy etc. from Matlab
  • Python executable choice persists across Matlab sessions–Matlab “remembers” even after you restart Matlab or reboot the computer.
  • editing imported Python module code requires restarting Matlab to take effect.

reference

Older versions of Matlab back to R2014b can also call Python from Matlab, but is more difficult to use, particularly for arrays / matrices. Matlab ≥ R2018b is strongly recommended for use with Python.

Matlab versionPython version
R2018aup to 3.6
R2017bup to 3.6

Troubleshooting

If you get

Error using pyversion Path argument does not specify a valid executable.

Ensure the Python executable path in file matlab.settings matches the desired Python executable. On Linux, this file is at ~/.matlab/R2018b/matlab.settings.

matlab.settings must have a section like:

<settings name="external">
    <settings name="interfaces">
        <settings name="python">
            <key name="Version">
                <string minArraySize="4" maxArraySize="4">
                    <value><![CDATA[3.7]]></value>
                    <value><![CDATA[/home/username/anaconda3/bin/python]]></value>
                    <value><![CDATA[/home/username/anaconda3]]></value>
                    <value><![CDATA[/home/username/anaconda3/lib/libpython3.7m.so]]></value>
                </string>
            </key>
        </settings>
    </settings>
</settings>

reference