Skip to content
Snippets Groups Projects
py-packages_installation.rst 6.40 KiB

Installing Python Packages for Geospatial Data Analysis

The purpose of this page is to walkthrough installing Python and Python packages that are needed to perform various GIS-tasks.

How do I begin?

Install Anaconda which is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment, and virtual environments. In short, it makes life easier when working with Python.

Install Anaconda on Windows

Download Anaconda installer (Latest Python version, 64 bit) for Windows.

Install Anaconda to your computer by double clicking the installer and install it into a directory you want (needs admin rights). Install it to all users and make sure the "add to PATH" box is checked.

Test that the Anaconda package manager called conda works by opening the newly downloaded Anaconda Prompt and running the command conda --version.

Install Anaconda on Mac / Linux

Download Anaconda installer (Latest Python version, 64 bit) for Mac or Linux.

Test that the Anaconda package manager called conda works by opening Terminal on your Mac or Linux machinne and running the command conda --version.

Add Anaconda to system path

# Add Anaconda installation permanently to PATH variable
nano .bash_profile

# Add following line at the end of the file and save (EDIT ACCORDING YOUR INSTALLATION PATH)
export PATH=$PATH:/PATH_TO_ANACONDA/anaconda3/bin:/PATH_TO_ANACONDA/anaconda3/lib/python3.7/site-packages

Install Jupyter Notebook

Per their own documentation , "Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more."

We use this a lot, and will use this in other tutorials so you will want to install this. You can install Jupyter Notebook using conda:

conda install -c conda-forge notebook

After installation is completed, you can start a Jupyter Notebook instance by running this command:

jupyter notebook

Your current Anaconda Prompt/Terminal shell will then run a command to launch Jupyter Notebook in a new browser window. Jupyter Notebook will be relying on the Anaconda Prompt/Terminal shell that launched it, so if you need to enter additional bash/conda commands just open another Anaconda Prompt/Terminal shell, or you can press CTRL+C on the current shell to interrupt the kernel, shutdown Jupyter Notebook, and enter commands again. Jupyter Notebooks save with the file extension .ipynb, short for IPython Notebook. (Note: to open one of these files, you will need to first make sure jupyter notebook has been launched from either Anaconda Prompt(Windows) or Terminal(Mac), and then navigate to it in the browser window.)

Install Python Packages

Install GIS related packages with conda (and pip) by running in command prompt following commands (in the same order as they are listed): You can start by installing geopandas, and add other packages once you start needing them. (Note: installing geopandas will automatically install several dependencies, or other python packages (such as Shapely and fiona) on your computer, so you will have a nice setup already using one installation command!)

Install packages

conda install -c conda-forge geopandas
conda install -c conda-forge matplotlib
conda install -c conda-forge geojson
conda install -c conda-forge folium
conda install -c conda-forge ipyleaflet
conda install -c conda-forge rasterio

You can test that the installations have worked by running following commands in a new Jupyter Notebook (try importing only those packages you installed):

import geopandas as gpd
import matplotlib
import geojson
import folium
import ipyleaflet
import rasterio

If you don't receive any errors, everything should be working!

How to find out which conda -command to use when installing a package?

The first thing to try when installing a new module X is to run in a command prompt (as admin) following command (here we try to install a hypothetical module called X)

conda install X

or downloading from a specific channel:

conda install X -c conda-forge

In most cases this approach works but sometimes you may need to google the package name + 'anaconda cloud' to find the correct command to run. Here is an example anaconda cloud page with more information on how to install GeoPandas.

Creating a new environment using conda

Conda allows us to create different Python environments . Python packages have often many dependencies, and some packages might, for example, only work with an older version of Python.

Run this codes in order to get started with a new Python environment (tested to work on Windows 10 in November 2019):

# Create a new environment with python 3.7 (feel free to change 'new_env' to whatever name you like!)
conda create -n new_env python=3.7

# Activate the environment
conda activate new_env

# Install jupyter notebook again, because there are no packages on this new environment
conda install -c conda-forge notebook

# Start jupyter notebook
jupyter notebook

# Jupyter notebook opens up in a browser

# Shut down jupyter notebook using CTRL+C once done working

Here is a "cheat sheet" of Anaconda commands for your reference.

Instructions on this page have been adapted from Automating GIS processes -course @ Department of Geosciences & Geography, University of Helsinki