Running cartopy under binder

Hi,

I’m trying to get this repo:

to run under mybinder but it keeps crashing during the setup of Cartopy:

Collecting Cartopy==0.18.0
Downloading Cartopy-0.18.0.tar.gz (14.4 MB)
ERROR: Command errored out with exit status 1:
command: /srv/conda/envs/notebook/bin/python3.7 -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"’"’/tmp/pip-install-u56n8zts/Cartopy/setup.py’"’"’; file=’"’"’/tmp/pip-install-u56n8zts/Cartopy/setup.py’"’"’;f=getattr(tokenize, ‘"’"‘open’"’"’, open)(file);code=f.read().replace(’"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, file, ‘"’"‘exec’"’"’))’ egg_info --egg-base /tmp/pip-pip-egg-info-h_3_nj6e
cwd: /tmp/pip-install-u56n8zts/Cartopy/
Complete output (12 lines):
Traceback (most recent call last):
File “/tmp/pip-install-u56n8zts/Cartopy/setup.py”, line 43, in
import numpy as np
ModuleNotFoundError: No module named ‘numpy’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-install-u56n8zts/Cartopy/setup.py", line 45, in <module>
    raise ImportError('NumPy 1.10+ is required to install cartopy.')
ImportError: NumPy 1.10+ is required to install cartopy.
----------------------------------------

ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
Removing intermediate container c697955c23c4
The command ‘/bin/sh -c ${KERNEL_PYTHON_PREFIX}/bin/pip install --no-cache-dir -r “requirements.txt”’ returned a non-zero code: 1

cartopy is the last line in my requirements.txt so everything else should already be available at that point.

Any suggestions are most welcome. I haven’t really be able to find anything helpful online.

Best,

Bruno

I don’t know about conda but pip doesn’t respect package order in requirements.txt when deciding what order to install stuff in.

One workaround I have used it to do package installation via postBuild, installing a ‘must-do-this-first’ package explicitly in one step and then invoking the install according to requirements.txt in a later step.

So in postBuild something like:

pip install numpy
pip install -r ./requirements.txt

Another way to force the package install order is to create the Binder environment using a Dockerfile, installing your ‘must-do-this-first’ package explicitly before the more general install from a full requirements/environment file.

1 Like

You can also experiment with quicker build times and persistent logs by using repo2docker locally https://repo2docker.readthedocs.io If you can get it working with repo2docker, then it’ll work on Binder as it is the same tool that works in the background :slight_smile:

1 Like

cartopy has pretty extensive build-time dependencies and doesn’t express any of them as build dependencies in setup.py (some of them, it probably can’t, such as GEOS). That, combined with the fact that they don’t provide pre-built wheels (again, probably very hard to do, given dependencies), essentially means that it is not pip-installable without substantial manual pre-install steps.

The result, I’d say, is to follow cartopy’s own documentation:

The easiest way to install Cartopy is by using Conda

and replace your requirements.txt with a conda environment.yml:

dependencies:
  - matplotlib==3.1.3
  - networkx=2.4
  - numpy==1.18.1
  - pandas==1.0.1
  - scipy==1.4.1
  - tqdm==4.46.0
  - watermark==2.0.2
  - python-wget==3.2
  - shapely
  - Cartopy==0.18.0

I opened this PR to make the switch, and it seems to build: try it out.

1 Like

This is excellent. Thank you so much. I’m still tarting to wrap my head around mybinder and wasn’t aware of the differences between pip and conda environments.

Thank you. I’ll start using it to test things locally.

From the logs I can see that numpy was already installed, so it wasn’t just a matter of installation order. Switching to using conda instead seems to have fixed the problem.