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

R2018b3.5, 3.6
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
  1. create Python 3.6 virtual environment (assuming Miniconda Python)
   conda create -n py36 python==3.6
   conda activate py36
  1. 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

  1. verify Matlab Python config by typing pyversion from within Matlab:
        version: '3.6'
    executable: '~/miniconda3/envs/py36/bin/python'
       library: '~/miniconda3/envs/py36/lib/'
          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
   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:


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


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

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


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



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">