How to install matplotlib basemap on Ubuntu/Debian to fix ModuleNotFoundError: No module named 'mpl_toolkits.basemap'

Note that matplotlib basemap is deprecated in favour of cartopy !

Problem:

When running your Python script, you see this error message:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-b1872316c43a> in <module>
----> 1 from mpl_toolkits.basemap import Basemap

ModuleNotFoundError: No module named 'mpl_toolkits.basemap'

But installing basemap using pip install basemap doesn’t work:

ERROR: Could not find a version that satisfies the requirement basemap (from versions: none)
ERROR: No matching distribution found for basemap

Solution

First, we need to install the libgeos library and development headers:

sudo apt -y install libgeos-dev

Now we can install basemap using

pip3 install git+https://github.com/matplotlib/basemap/

This will usually take a couple of minutes.

Now run your script again, the ModuleNotFoundError should have disappeared now.