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, which added significant functionality and performance
Matlab Python
R2018b 3.5, 3.6
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
  1. create Python 3.6 virtual environment (assuming Miniconda Python)
    conda create -n py36 python==3.6
       
    conda activate py36
    
  2. the exact location of your Python executable (to put in pyversion()) is determined by:
    python -c "import sys; print(sys.executable)"
    

    let’s assume your Python 3.6 executable is at ~/miniconda3/envs/py36/bin/python. Setup Matlab to use Python 3.6 by adding to ~/Documents/MATLAB/startup.m

    pyversion('~/miniconda3/envs/py36/bin/python')
    
  3. verify Matlab Python config by typing pyversion from within Matlab:
         version: '3.6'
     executable: '~/miniconda3/envs/py36/bin/python'
        library: '~/miniconda3/envs/py36/lib/libpython3.6m.so'
           home: '~/miniconda3/envs/py36'
       isloaded: 0
    

Install Python user programs

This example is for the IRI2016 ionospheric Fortran model. IRI2016 compiled Fortran modules is accessed via Python in Matlab, on Linux, Mac or Windows. Here we assume you work under directory ~/code

  1. setup Python user module, from Terminal or Command Prompt:
    cd ~/code
       
    git clone https://github.com/scivision/iri2016
    cd iri2016
       
    python -m pip install -e .[tests]
    

    The pip command automatically invokes the Fortran compiler on your system, if Fortran code is included in the particular Python module you’re using.

  2. Before trying to use IRI2016 from Matlab, be sure the Python code is working by from Terminal in the ~/code/iri2016 directory typing:
    pytest
    

    If this makes errors, they will need to be resolved before IRI2016 will work in Matlab!

Troubleshooting

If the Python module has compiled code, you will need compilers:

  • Linux: apt install gcc g++ gfortran
  • Mac: brew install gcc
  • Windows

Notes

  • Python module import is implicit in the Matlab → Python module function call
  • Python executable choice persists across Matlab sessions.
  • editing imported Python module code requires restarting Matlab to take effect.

reference

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

Tags:

Categories:

Comments

Written by Michael Hirsch, Ph.D. //