Import Python user modules in Matlab

1 minute read

This example shows how to:

  1. setup Matlab with user Python install (e.g. Anaconda Python)
  2. import Python user modules (e.g. made yourself or from Github).

Example assumes Anaconda/Miniconda Python.

  1. setup Python user module. Let’s say you want to use ESPRIT in Python from Matlab
    git clone https://github.com/scivision/signal_subspace
    cd signal_subspace
    python -m pip install -e .
    
  2. from Matlab, choose the Python executable to use. Note, you must restart Matlab if you subsequently want to pick another Python install.
    pyversion ~/miniconda/bin/python
    py.signal_subspace.esprit()
    

    Python Error: TypeError: esprit() missing 2 required positional arguments: ‘x’ and ‘L’

Switching Python version

Matlab R2018b supports Python versions 2.7, 3.5 and 3.6. If you have multiple Python versions installed through Anaconda Python, you can switch between Python versions. Note that after Python isloaded==1, Matlab must be restarted for another Python version to be selected.

Suppose that Python 3.7 is the default on the system:

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

Thus you may need to setup Python 3.6 if your usual workflow is with Python 3.7 etc.

  1. create Python 3.6 virtual environment (assuming Anaconda Python)
    conda create -n py36 python=3.6
    
  2. set Matlab to Python 3.6 by restarting Matlab and then in Matlab
    pyversion ~/miniconda3/envs/py36/bin/python
    
  3. verify Matlab Python config by typing pyversion:
         version: '3.6'
     executable: '~/miniconda3/envs/py36/bin/python'
        library: '~/miniconda3/envs/py36/lib/libpython3.6m.so'
           home: '~/miniconda3/envs/py36'
       isloaded: 0
    

Notes

  • you do not import Python modules with Matlab, the Python module import is implicit in the module function call
  • Python executable choice persists across Matlab sessions.

reference

Troubleshooting

If you get

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

To fix by manually overriding the Python executable path, in file matlab.settings. 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/joe/anaconda3/bin/python]]></value>
                    <value><![CDATA[/home/joe/anaconda3]]></value>
                    <value><![CDATA[/home/joe/anaconda3/lib/libpython3.6m.so]]></value>
                </string>
            </key>
        </settings>
    </settings>
</settings>

Note: You can force Python 3.6 to work with Matlab < R2017b this way.

reference

Tags:

Categories:

Updated:

Leave a comment