How to set up Python for geospatial science and computing

This tutorial explains how to install Python and useful packages for geospatial science and computing on MS Windows as a non-admin user.

1   Install Python

First and foremost, you need to install Python of course.

  1. Download the latest version of the installer from https://www.python.org/downloads/. Please use the recommended download (e.g., python-3.9.5-amd64.exe).
  1. Run the installer.
  2. Don’t install launcher for all users. You cannot use this option as a non-admin user.
  3. Add Python x.y to PATH. This option will make your life a little bit easier later.
  4. Customize installation.
  5. Next.
  6. Customize install location. You may want to use the P drive so you can run this installation remotely from another computer. For example, P:\opt\Python39.
  7. Install.

2   Install NumPy

Now, your Python is ready. Let’s install the NumPy package using pip. NumPy provides fundamental tools for scientific computing with Python.

  1. Hit Win + R.
  2. Type cmd.
  3. pip install numpy

3   Upgrade pip

I’m getting a warning message about the pip version. It’s optional, but let’s upgrade pip.

python -m pip install --upgrade pip

4   Install Matplotlib

Matplotlib is a plotting package for Python.

pip install matplotlib

5   Install GDAL

The Geospatial Data Abstraction Library (GDAL) is probably one of the most important open-source geospatial libraries along with PROJ, coordinate transformation software. The GDAL package for Python is a wrapper around the GDAL library. Running pip install GDAL fails because its compile environment is not set up. However, we can install an unofficial compiled package (wheel) from https://www.lfd.uci.edu/~gohlke/pythonlibs/.

  1. Download the wheel file for your Python version from https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal. For example, if your Python version is 3.9.1, download GDAL-3.3.0-cp39-cp39-win_amd64.whl. Here, cp39 means CPython (the C implementation of Python that we installed above) 3.9.
  2. Do you know where you downloaded this file? Change to that directory from the command window. For example, if you downloaded it to C:\Users\USERNAME\Downloads,
    C:
    cd C:\Users\USERNAME\Downloads
  3. Now, install the wheel file:
    pip install GDAL-3.3.0-cp39-cp39-win_amd64.whl

6   Install JupyterLab

JupyterLab is the next generation of Jupyter, a language-agnostic web-based notebook that supports Python and other languages. Installing JupyterLab will install Jupyter and IPython, an interactive shell for Python.

pip install jupyterlab

Run it:

jupyter-lab

It will start a local web server from where you started it and open a new tab in the browser. Avoid running it from C:\ unless you’re an admin because non-admin users cannot create files in C:\.

If you want to kill this JupyterLab instance, hit Ctrl+C from the command window. Remember, it “kills” JupyterLab, so you want to save all your work first.

You can try Jupyter:

jupyter notebook

It looks similar to JupyterLab, but it will open new browser tabs for new notebooks unlike JupyterLab that implements its own tabbing environment.

Let’s try IPython as well:

ipython

7   Homework: Show me your JupyterLab

Set up your Python environment by following the above instructions and start JupyterLab.

  1. Create a new Python 3 Notebook.
  2. Type this code in a cell:
    name = "FirstLastname"
    print("Hello " + name)
  3. Run it by hitting Shift+Enter.
  4. Take a screenshot of the result and submit it in FirstLastname_JupyterLab.png.