0

I'm trying to run a script within a Google datalab Jupyter notebook that requires basemap, but I can't install this package. Yelsayed has pointed out that the module requires several dependencies, but even after building these the module won't install.

Here are the dependencies that I believe need to be installed:

!pip install Cython

!apt-get update && apt-get install -y gcc

!pip install pyproj

!pip install GEOS

These install without event. Finally basemap itself which again Yelsayed has pointed out needs to be done with the following:

!pip install https://downloads.sourceforge.net/project/matplotlib/matplotlib-toolkits/basemap-1.0.7/basemap-1.0.7.tar.gz

This takes time to install and seems to show promise, but ultimately it gets to 99% installed and then outputs "killed":

Collecting https://downloads.sourceforge.net/project/matplotlib/matplotlib-toolkits/basemap-1.0.7/basemap-1.0.7.tar.gz
Downloading https://downloads.sourceforge.net/project/matplotlib/matplotlib-toolkits/basemap-1.0.7/basemap-1.0.7.tar.gz (129.7MB)
99% |############################### | 129.7MB 154kB/s eta 0:00:01Killed

The "Killed" at end seems to indicate it ultimately hasn't worked. It does this reliably, re-attempting the above call fails in the same way. In any case I gave importing the module a go, but still get the same error:

ImportError: No module named basemap

Why is this happening and how do you successfully install basemap in this environment?

goose
  • 2,502
  • 6
  • 42
  • 69

3 Answers3

4

It doesn't look like basemap is hosted on PyPI due to size. Look at the install instructions on https://github.com/matplotlib/basemap.

You can just use their direct link for v1.0.7:

!pip install https://downloads.sourceforge.net/project/matplotlib/matplotlib-toolkits/basemap-1.0.7/basemap-1.0.7.tar.gz

EDIT

Here's some more breakdown of installing the prerequisites as well, run all the following in notebook cells:

GEOS:

Your best bet is to install it directly from apt to avoid versioning issues. You can do:

%bash
apt-get update && apt-get install -y build-essential libgeos-3.4.2 libgeos-c1 libgeos++-dev

pyproj, pyshp

You then install the python dependencies, using pip easily:

%bash
pip install pyproj pyshp

basemap

Then you can install the basemap package using the link above:

%bash
!pip install https://downloads.sourceforge.net/project/matplotlib/matplotlib-toolkits/basemap-1.0.7/basemap-1.0.7.tar.gz

You can then import the basemap class:

from mpl_toolkits.basemap import Basemap
yelsayed
  • 5,236
  • 3
  • 27
  • 38
  • Thanks for the answer. It seems very promising when this runs, it takes a while to install, but at the end it say "killed" and the won't load I get "ImportError: No module named basemap" I'll post a fuller trace in my question. – goose Feb 06 '17 at 20:41
  • @goose do you have all the requirements installed as well? on their github repo there's a list of at least three things that you need to install that don't exist as part of Datalab – yelsayed Feb 06 '17 at 20:45
  • Thanks again for all the help @yelsayed. I hadn't spotted that. I've working through it now, just struggling to install pyproj – goose Feb 06 '17 at 21:51
  • well it's not easy to install lots of requirements through the notebook interface, we're aware of this. hopefully in the future there would be a way to put your requirements in a custom script that is loaded when the Datalab instance starts. you can open an issue here: github.com/googledatalab/datalab if you have a specific use case. – yelsayed Feb 07 '17 at 02:36
  • Thanks @Yelsayed. I'm not sure if it's a use case, but I've posted what I'm stuck on here: http://stackoverflow.com/questions/42098899/cant-install-pyproj-module-in-google-datalab-jupyter-notebook – goose Feb 07 '17 at 20:03
  • Unfortunately @Yelsayed after installing pyproj, GEOS and Cython the package basemap still doesn't install. It looks like it has, it gets to 99% as per above but ultimately says that it's killed. Attempts to load the module then fail. – goose Feb 07 '17 at 22:19
  • @goose you're probably having issues with some of the dependencies. take a look at the updated answer. – yelsayed Feb 11 '17 at 08:46
  • thanks very much for coming back to me. I ran the above, also adding in apt-utils. I ran the basemap installation with the %bash and got "bash: line 1: 1453 Killed". Running with an exclamation mark and without the %bash returns the same output as before, it seems promising, but ends on killed. The dependencies install without warning with the exception of a notification about apt-utils that I went back and installed. – goose Feb 11 '17 at 09:09
  • interesting. it worked fine for me. if you want to play around and experiment, an easier way is to open a shell session inside the docker container running Datalab. if you're running the container locally, you can do `docker ps` to get the container ID, then `docker exec -it bash` to get a shell, then try installing from there. also, do you see the package downloading then failing to install? – yelsayed Feb 11 '17 at 20:17
  • That's interesting to know, when installing from the shell as you directed I get exactly he same result. As you say, it downloads and fails to install with the output listed above. – goose Feb 11 '17 at 22:57
  • I think I've managed to install it. Using the shell that you told me about, I cloned & installed directly from git. It now loads. Thanks again for your help! It may be worth adding this to the solution above for others who follow. – goose Feb 12 '17 at 08:29
3

You can solve the problem writing this piece of code in your Google Cloud Datalab Notebook

! apt-get -y --allow-unauthenticated --fix-missing install python-mpltoolkits.basemap python-numpy python-matplotlib
1

Basemap doesn't come with the google datalab out of the box.

As of Feb 2019, this works on a fresh google datalab:

Step 1: Install Pre-requisites

  • !apt-get update && apt-get install -y --allow-unauthenticated build-essential libgeos-3.5.0 libgeos-c1v5 libgeos++-dev

Note: I tried yelsayed's example, however my datalab couldn't find libgeos-3.4.2 and libgeos-c1 was deprecated at the time I ran it. I updated these values to 3.5.0 and c1v5 and it worked. I also had to add the allow unauthenticated tags part.

  • !pip install pyproj pyshp

Step 2: Install the whole package

  • !pip install https://downloads.sourceforge.net/project/matplotlib/matplotlib-toolkits/basemap-1.0.7/basemap-1.0.7.tar.gz

Step 3: Check the package has been installed correctly

  • !pip freeze

Step 4: Import the module

  • from mpl_toolkits.basemap import Basemap
Zack
  • 101
  • 1
  • 5