Problem importing a module (mpl_toolkits.basemap) in Python
So the issue is this: I'm attempting to learn how to plot maps with Python, using shapefiles. There are plenty of examples available on the internet, but just about all of them start with something like
from mpl_toolkits.basemap import BasemapWhich, when I attempt to input this into an IPython notebook, the Python console, or Pycharm, returns
ImportError: No module named 'mpl_toolkits.basemap'To which the next response is, okay, time to sudo apt-get install python-mpltoolkits.basemap. That runs, returning:
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-mpltoolkits.basemap is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 12 not upgraded.I tried sudo apt-get remove python-mpltoolkits.basemap and then re-installing with apt-get, no joy.
How can I either get the system to recognize that the module exists, or to re-install it in such a way that it's recognizable to the Python interpreter?
14 Answers
This article describes how to install basemap. And Here is the git repo, and here is a browser downloadable source code.
From the article, in short:
1) Download the source if apt-get install does not work out
2) install the source (super useful if installing to a virtual environment) by:
cd basemap-1.x.x
python setup.py installThen here is also the doc, with installation guides and how to test if you have installed correctly. (just run from mpl_toolkits.basemap import Basemap in terminal).
It's important to note that if you're having issues with libgeos being detected, the doc suggests that you install the included libgeos source (3.3.3) separately first.
f you're using Anaconda, it has a package for basemap:
conda install basemap(It looks like pip doesn't have the package any more.)
3In my case, the following command worked
sudo apt install python3-mpltoolkits.basemap first update your conda
$ conda update condacheck your python version
$ conda search "^python$"creat a virtual environment for your project
$ conda create -n yourenvname python=x.x anacondaactivate your environment
$ source activate yourenvironmentinstall the basemap package in your environment
$ conda install -n yourenvname basemap