How to Fix the “No module named ‘mpl_toolkits.basemap'” Error in Python

When working with the geographic data and plotting the maps in Python we might encounter the error:

ModuleNotFoundError: No module named ‘mpl_toolkits.basemap’

This error occurs because of the mpl_toolkits.basemap module which is part of the base map toolkit is not installed in the Python environment. In this article, we will go through the steps to fix this error by installing the basemap toolkit.

Understand the Basemap Toolkit

The Basemap is a library in the matplotlib toolkit that allows for the creation of geographical maps. It extends Matplotlib by adding geographic projections and some map plotting capabilities.

Fix the “No module named ‘mpl_toolkits.basemap'” Error in Python

Check Python and Pip Versions

Before installing any new packages it’s a good practice to the check the Python and pip versions. We can do this by the running:

python –version

pip –version

Ensure that you have pip installed and that your Python version is compatible with the basemap. The Basemap works with the Python 3.6 and later.

Install Dependencies

The Basemap requires several dependencies. We need to the install numpy, matplotlib, pyproj and Pillow. Install these dependencies using pip:

pip install numpy matplotlib pyproj six Pillow

Install Basemap

As of the time of writing, basemap is not available on the PyPI in which means you need to the install it using an alternative method, such as conda or directly from the GitHub repository.

Using Conda

If you have conda installed we can install basemap using the following command:

conda install -c conda-forge basemap

Using Pip and GitHub

If you prefer using pip, you can install basemap directly from the GitHub repository. Run the following commands:

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

pip install git+https://github.com/matplotlib/basemap-data.git

Verify Installation

To verify that basemap is installed the correctly we can run a simple script that imports Basemap and plots a basic map.

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
def draw_map():
map = Basemap(projection='merc', llcrnrlat=-60, urcrnrlat=90, llcrnrlon=-180, urcrnrlon=180, resolution='c')
map.drawcoastlines()
map.drawcountries()
plt.show()
if __name__ == "__main__":
draw_map()

If the script runs without the errors and displays a map and it means basemap is installed correctly.

Troubleshooting

If you still encounter the “No module named ‘mpl_toolkits.basemap'” error after following the above steps consider the following troubleshooting tips:

Check Virtual Environment: Ensure you are installing and running the script in the same virtual environment.

python -m venv myenv

source myenv/bin/activate # On Windows use `myenv\Scripts\activate`

pip install numpy matplotlib pyproj six Pillow

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

pip install git+https://github.com/matplotlib/basemap-data.git

Upgrade Pip: Make sure you have the latest version of pip.

pip install –upgrade pip

Reinstall Basemap: Sometimes a fresh installation can resolve issues.

pip uninstall basemap

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

pip install git+https://github.com/matplotlib/basemap-data.git

Check for Typographical Errors: The Ensure there are no typos in the import statements or installation commands.

Conclusion

The “No module named ‘mpl_toolkits.basemap'” error can be resolved by the ensuring that all dependencies are installed and that basemap is correctly installed using the conda or pip. Following the steps outlined above should help the set up basemap and avoid common installation issues. With basemap installed we can now create detailed and informative geographical maps in Python.