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.

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
R2019a3.5, 3.6, 3.7
R2018b3.5, 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.

select Python version to run from Matlab

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

  1. 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
  2. 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')
  3. 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
    

Install Python user programs

This example is for the “FindSSH” Python program, which concurrently searches out SSH servers on an IPv4 subnet. It’s useful to find IoT or Raspberry Pi etc. IP addresses quickly.

  1. setup Python user module, from Terminal or Command Prompt:

    git clone https://github.com/scivision/findssh
        
    cd findssh
    
    python -m pip install -e .[tests]   
  2. Check the Python code is working by itself from Terminal:

    python -m pytest   

    If this makes errors, they will need to be resolved before a Python user library will work in Matlab!

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 versionPython version
R2018a3.5, 3.6
R2017b3.4, 3.5, 3.6
R2017a3.4, 3.5
R2016b3.3, 3.4, 3.5
R2016a3.3, 3.4
R2015b3.3, 3.4
R2015a3.3, 3.4
R2014b3.3

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.6]]></value>
                    <value><![CDATA[/home/username/anaconda3/bin/python]]></value>
                    <value><![CDATA[/home/username/anaconda3]]></value>
                    <value><![CDATA[/home/username/anaconda3/lib/libpython3.6m.so]]></value>
                </string>
            </key>
        </settings>
    </settings>
</settings>

reference