Howto: Install Python for geospatial applications
By: Christoph Rieke
Tags: Remote Sensing, GIS, Anaconda, Geospatial, Python

This Howto gives step for step instructions on setting up a Python 3 environment on Windows with gdal, rasterio, geopandas and other essential geospatial libraries.

Installing Python 3.7 with Anaconda for Windows

Uninstall any remaining Python Anaconda installations via Start > “Apps & features”.

Download the Python 3.7 - 64-Bit Graphical Installer from the Anaconda website.

Install it using the default options, except select “C:\Anaconda3” as the installation path, and check “Add Anaconda to Path”. By default Anaconda Python would be installed to Windows AppData, setting the installation path to a folder on the “C:” drive is just for convenience when configuring paths and environments.

Open the environment variables (just search in the Windows start menu. If you are not admin by default on your system, use right-click > Open as administrator). In the bottom section under “System variables” check that the “Path” variable includes the following paths. If not, add them by using the “New” button.

(just search in the Windows start menu. If you are not admin by default on your system, use right-click > Open as administrator). In the bottom section under “System variables” check that the “Path” variable includes the following paths. If not, add them by using the “New” button. C:\Anaconda3

C:\Anaconda3\Scripts

Open the command-line via Start > “cmd” and run the following commands. Explanations and potential errors explained below respectively.

conda update -n base conda

This will update and install the latest conda version. If you encounter any “SSL verification” related errors using conda commands, the most likely cause is your company’s network. You can either disable the SSL verification via “conda config — set ssl_verify no” and repeat the step above; or talk to your IT department and provide conda with the file path to a local SSL certificate.

conda config --add channels conda-forge

conda update --all

Here we add/give the highest priority to the “conda-forge” channel and update all Python standard library packages to the newest version. Conda has various channels from which it can install packages, “conda-forge” is community-managed and regarded as reliable and up-to-date.

Creating a virtual environment

A virtual environment acts as a “container” for your Python installation. If you are just starting out or don’t need to manage multiple projects that could require different dependencies, using virtual environments often seems unnecessary when you could just use the base installation. I personally still recommend to use an environment for another reason: Because you can clone/backup your working environment, you will have a lot less frustration when something breaks your Python installation.

conda create --name geo_py37 python=3.7

This create a new virtual environment called “geo_py37” (pick any name you like). Anytime you want to use that specific environment, you will need to activate it via

activate geo_py37

Installing GDAL

I have seen quite a lot of people getting frustrated by problems stemming from version and dependency issues related to GDAL & rasterio when installing other libraries at the same time. Thus, just to be save, we will start with these two.

conda install gdal

GDAL requires a pointer to a specific folder containing projection and transformation information, otherwise you will run into errors when using some functions. In the Windows environments variable menu, under “User variables” in the top section, create a new “GDAL_DATA” variable pointing to “C:\Anaconda3\envs\geo_py37\Library\share\gdal”.

conda install rasterio

You can test the gdal & rasterio installation via:

activate geo_py37

python

import gdal

import rasterio exit()

Installing the rest of the packages

Install all other packages using one long command:

activate geo_py37

conda install numpy pandas geopandas scikit-learn jupyterlab matplotlib seaborn xarray rasterstats tqdm pytest sqlalchemy scikit-image scipy pysal beautifulsoup4 boto3 cython statsmodels future graphviz pylint mlxtend line_profiler nodejs sphinx nbsphinx

This will, among others, also install the pyproj library as a dependency. Just like with GDAL, create a new user variable for it: “PROJ_LIB” pointing to “C:\Anaconda3\envs\geo_py37\Library\include\boost\geometry\srs\projections\proj”

A few other packages are not available via conda, we install them via pip:

pip install sentinelsat mercantile

In case pip is blocked by your company network, use this instead:

pip install sentinelsat mercantile --trusted-host pypi.org --trusted-host files.pythonhosted.org

Finished!

Some other ressources I can recommend: Python for Geospatial work flows Part 1–3 as well as Python environments and how to manage them with Conda.

If you have any feedback or encounter problems using these instructions please leave a comment or send me a message on Twitter @ chrieke