35

It seems that I cannot get pip to install Cartopy on my computer. I work straight from the windows command line (no Anaconda or other proxy programs).

When I try "pip install cartopy" I get the expected:

C:\Users\Justin\Documents\Python Programs>pip install cartopy
Collecting cartopy
  Using cached https://files.pythonhosted.org/packages/e5/92/fe8838fa8158931906dfc4f16c5c1436b3dd2daf83592645b179581403ad/Cartopy-0.17.0.tar.gz
  Installing build dependencies ... done
    Complete output from command python setup.py egg_info:
    C:\Users\Justin\AppData\Local\Temp\pip-install-cetb0vj7\cartopy\setup.py:171: UserWarning: Unable to determine GEOS version. Ensure you have 3.3.3 or later installed, or installation may fail.
      '.'.join(str(v) for v in GEOS_MIN_VERSION), ))
    Proj 4.9.0 must be installed.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\Justin\AppData\Local\Temp\pip-install-cetb0vj7\cartopy\

I know that "pip install proj" does not actually get the correct module version so I went to https://proj4.org/install.html to download and install the OSGeo4W which I thought would solve my problem but has appeared not to (for reference I do still have this on my computer though).

So then I tried install the cartopy .whl directly from this https://www.lfd.uci.edu/~gohlke/pythonlibs/#cartopy website and tried using "pip install Cartopy-0.17.0-cp37-cp37m-win32.whl" (I use the the 32 bit version of Python3.7 so I am fairly certain this is the correct file). But I then get the error:

C:\Users\Justin\Documents\Python Programs>pip install Cartopy-0.17.0-cp37-cp37m-win32.whl
    Processing c:\users\justin\documents\python programs\cartopy-0.17.0-cp37-cp37m-win32.whl
        Requirement already satisfied: numpy>=1.10 in c:\users\justin\appdata\local\programs\python\python37\lib\site-packages (from Cartopy==0.17.0) (1.15.1)
        Requirement already satisfied: setuptools>=0.7.2 in c:\users\justin\appdata\local\programs\python\python37\lib\site-packages (from Cartopy==0.17.0) (40.6.2)
        Requirement already satisfied: six>=1.3.0 in c:\users\justin\appdata\local\programs\python\python37\lib\site-packages (from Cartopy==0.17.0) (1.11.0)
        Collecting pyshp>=1.1.4 (from Cartopy==0.17.0)
          Downloading https://files.pythonhosted.org/packages/08/3e/3bda7dfdbee0d7a22d38443f5cc8d154ff6d4701e615f4c07bf1ed003563/pyshp-2.0.1.tar.gz (214kB)
            100% |████████████████████████████████| 215kB 1.4MB/s
        Collecting shapely>=1.5.6 (from Cartopy==0.17.0)
          Using cached https://files.pythonhosted.org/packages/a2/fb/7a7af9ef7a35d16fa23b127abee272cfc483ca89029b73e92e93cdf36e6b/Shapely-1.6.4.post2.tar.gz
            Complete output from command python setup.py egg_info:
            Traceback (most recent call last):
              File "<string>", line 1, in <module>
              File "C:\Users\Justin\AppData\Local\Temp\pip-install-sb4uyliy\shapely\setup.py", line 80, in <module>
                from shapely._buildcfg import geos_version_string, geos_version, \
              File "C:\Users\Justin\AppData\Local\Temp\pip-install-sb4uyliy\shapely\shapely\_buildcfg.py", line 200, in <module>
                lgeos = CDLL("geos_c.dll")
              File "c:\users\justin\appdata\local\programs\python\python37\lib\ctypes\__init__.py", line 356, in __init__
                self._handle = _dlopen(self._name, mode)
            OSError: [WinError 126] The specified module could not be found

So then I tried to download something called a tar.gz file, but this is where I lose myself. I am not familiar with tar.gz and I saw there you have to "./configure" the file path, but I was not sure how to do this nor was I sure where I should store said file. Any guidance around this issue would be so incredibly appreciated thank you.

Justin Jones
  • 361
  • 1
  • 3
  • 7

7 Answers7

44

According to response to the corresponding Github issue, https://github.com/googlecolab/colabtools/issues/85#issuecomment-372532728, ubuntu users need to install some extra packages before installing Cartopy.

sudo apt-get install libproj-dev proj-data proj-bin  
sudo apt-get install libgeos-dev  
sudo pip install cython  
sudo pip install cartopy  
Pim van der Heijden
  • 6,956
  • 4
  • 16
  • 21
Chen Yazheng
  • 449
  • 4
  • 3
  • 4
    had to add `sudo apt install python3-dev` in order to work properly – Vitor Santos Aug 03 '20 at 21:11
  • 5
    `sudo pip install` seems unadvisable – Jortstek Nov 25 '21 at 18:42
  • 4
    This installs `proj=6.3.1` but cartopy now requires `proj=8.0.0` – Silver Apr 08 '22 at 01:21
  • 1
    ... working from the python:3.10 Docker image, I get "Unable to locate package" apt-get errors for all four of these packages: libproj-dev, proj-data, proj-bin, libgeos-dev. I needed a "apt-get update" before they were recognized... but that was insufficient to install Cartopy. https://github.com/SciTools/cartopy/issues/1879 seems to suggest the appropriate solution is to build "proj" from source rather than through apt-get. – Sarah Messer Apr 08 '22 at 14:22
  • This question is quite obviously on windows, you've posted an answer for linux. It also no longer works, as the latest version you can get with `apt-get` is `6.3.1`, and cartopy now requires `8.0.0`. [I've posted an answer here for linux users who face this problem.](https://stackoverflow.com/questions/72442087/cant-install-proj-8-0-0-for-cartopy-linux) – Recessive May 31 '22 at 04:04
  • On Windows I get this `ERROR: Could not find a version that satisfies the requirement libproj-dev (from versions: none)` – MERose Jul 15 '22 at 07:01
22

I strongly recommend installation using conda (not necessarily a full Anaconda install) as it will bring in all of the executable dependencies that cartopy requires. With conda installed, installing the latest version of cartopy would look like:

conda install --channel conda-forge cartopy

Unfortunately installation of binaries for Python is notoriously fiendish on Windows (partly because the toolchain changes with each Python version [at least, it used to until >= python 3.5], and partly because most of the developers of the scientific python ecosystem typically don't use or have access to Windows machines). Christoph Gohlke's incredible resource that you have already been using is the major exception to this statement.

I personally have spent several man-weeks getting cartopy installed on Windows (with significant help from folks including the Enthought team and Christoph Gohlke) - I poured all of this knowledge into the conda-forge packaging ecosystem so that others don't have to feel that pain if they choose to use conda.

If you really, really, really must install cartopy manually on your Windows machine, you might like to take a look at https://github.com/conda-forge/cartopy-feedstock/tree/master/recipe which contains all of the steps that were required to build cartopy in Windows. In addition to this, all of its dependencies are documented in similarly named repositories on the conda-forge organisation.

Finally, given your output and @cgohlke's suggestion - try installing his Shapely binary https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely.

pelson
  • 21,252
  • 4
  • 92
  • 99
  • The `conda install --channel conda-forge cartopy` should be ran as administrator on Windows. – Vityata Feb 19 '20 at 20:35
  • 1
    Conda has invariably, system independently, eventually got itself stuck in a loop, unable to install any package what-so-ever and needed a complete wipe from the system, and a purging of the python directory to get anything working again. It is not a viable long-term option in my experience – Silver Apr 08 '22 at 01:20
  • @Silver you can usually avoid this by installing [miniforge](https://github.com/conda-forge/miniforge#miniforge) or mambaforge, *NOT anaconda* and then keeping your base env clean and actually using environments. the problems occur when you try to install everything into a single monster base environment with a mix of channels. The Anaconda distribution often sets people up for failure here. But conda (esp. conda forge) is a great option for installing complex dependencies, and I've used it successfully for years on multiple OSs. – Michael Delgado Sep 02 '22 at 17:30
  • @pelson thanks for your work on this! it might be worth explicitly calling out that you should *not* run the command you specify in a vanilla anaconda install. this should be done in a clean (or at least, compatible) env, preferably with a clean base env too. – Michael Delgado Sep 02 '22 at 17:34
22

On a Mac I did this

brew install geos
brew install proj

Add this to top of /usr/local/include/proj_api.h

#define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H

pip install Cartopy

It works for Python3.6 but Fails for Python3.7.4

j-i-l
  • 10,281
  • 3
  • 53
  • 70
Tim Seed
  • 5,119
  • 2
  • 30
  • 26
2

For anyone else out there here is how I did it on Linux Mint 20.04 LTS using a python3 venv environment:

Activate the environment, then install the following:

sudo apt install python3-dev libproj-dev proj-data proj-bin libgeos-dev
pip3 install wheel numpy cartopy

That seemed to work fine for me.

themetman
  • 51
  • 6
1

I ran into multiple issues trying to install Cartopy inside a Docker container. This is the relevant part of the script I needed. Obviously needs some mods to run in a shell script.

FROM python:3.10

# Install support for Cartopy.
# See https://stackoverflow.com/questions/53697814/using-pip-install-to- 
install-cartopy-but-missing-proj-version-at-least-4-9-0
RUN apt-get update
RUN apt-get install -y libproj-dev proj-data proj-bin libgeos-dev python3-dev cmake sqlite3
RUN apt-get upgrade -y

# Install Proj from source since it's needed by Cartopy and unavailable via apt
# See https://github.com/SciTools/cartopy/issues/1879 and https://proj.org/install.html#compilation-and-installation-from-source-code
WORKDIR /tmp
RUN curl -sSL https://download.osgeo.org/proj/proj-8.2.1.tar.gz | tar -xvz -C /tmp
WORKDIR /tmp/proj-8.2.1
RUN mkdir build
WORKDIR /tmp/proj-8.2.1/build
RUN cmake ..
RUN cmake --build .
RUN cmake --build . --target install
RUN projsync --system-directory --list-files

After all that, I was able to pip install Cartopy 0.20.0 along with other packages.

Sarah Messer
  • 3,592
  • 1
  • 26
  • 43
1

If any problem arises while installing cartopy:

Downlaod Cartopy, shapely and pyprof from this website https://www.lfd.uci.edu/~gohlke/pythonlibs/ and install these using this commands in cmd terminal.

For example installing Cartopy

pip install "C:\Users\USER\Downloads\Cartopy-0.20.2-cp310-cp310-win_amd64.whl"

Surely it will work. Cheers!!

0

I made good experiences using the osdeo/gdal image when encountering docker issues with cartopy (and other geo libraries):

# select latest osgeo container
FROM osgeo/gdal:ubuntu-full-3.5.1

RUN apt-get update && \
    apt-get install -y \
    libgeos++-dev \
    libproj-dev \
    bash \
    build-essential \
    cmake \
    python3-pip \
    git-lfs \
    libeigen3-dev \
    libpq-dev \
    libhdf5-serial-dev \
    libnetcdf-dev \
    libomp-dev \
    libpng-dev \
    netcdf-bin \
    wget && \
    rm -rf /var/lib/apt/lists/*

# set up your working directory
WORKDIR /app

# Download and install dependencies with pip
COPY requirements.txt requirements.txt
RUN pip3 install --no-cache-dir --upgrade -r requirements.txt

# copy application 
COPY . .
# run app
CMD <appname>.py