## 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 version | Python version |
---|---|

R2019a | 3.5, 3.6, 3.7 |

R2018b | 3.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.

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`

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

setup Python user module, from Terminal or Command Prompt:

`git clone https://github.com/scivision/findssh cd findssh python -m pip install -e .[tests]`

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.

Older versions of Matlab back to R2014b can also call Python from Matlab, but is more difficult to use, particularly for arrays / matrices:

Matlab version | Python version |
---|---|

R2018a | 3.5, 3.6 |

R2017b | 3.4, 3.5, 3.6 |

R2017a | 3.4, 3.5 |

R2016b | 3.3, 3.4, 3.5 |

R2016a | 3.3, 3.4 |

R2015b | 3.3, 3.4 |

R2015a | 3.3, 3.4 |

R2014b | 3.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>
```