How to set up Python for geospatial science and computing

Dr. Huidae Cho
Institute for Environmental and Spatial Analysis...University of North Georgia

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.6-amd64.exe).
  2. Run the installer.
  3. Don’t install launcher for all users. You cannot use this option as a non-admin user.
  4. Add Python x.y to PATH. This option will make your life a little bit easier later.
  5. Customize installation.
  6. Next.
  7. Customize install location.
  8. Change the installation path to C:\Python39.
  9. Install.
  10. Close.

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.1-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   How to create .py files

You do have JupyterLab set up, right? In each cell, you type your code and Shift + Enter to run it. If the code works fine and you’re ready to save your code as a .py file, copy the content of that cell into an editor and save it as a new file.

How to copy a cell?

  1. Ctrl + A: to select all the content in a cell or use the mouse to select everything
  2. Ctrl + C: to copy the selected content into the clipboard
  3. Open an editor
  4. Ctrl + V: to paste the copied content

Please try this:

  1. Start JupyterLab
  2. Start a new Python 3 Notebook
  3. Type:
    x = 12
    print(x)
  4. Shift + Enter
  5. That runs, right?
  6. Click the cell again
  7. Ctrl + A
  8. Ctrl + C
  9. Open an editor
  10. Ctrl + V in the editor

Here, Ctrl + or Shift + means push Ctrl or Shift and hold then press whatever key that follows +.

8   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.