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.
- Download the latest version of the installer from https://www.python.org/downloads/. Please use the recommended download (e.g., python-3.9.7-amd64.exe).
- Run the installer.
- Don’t install launcher for all users. You cannot use this option as a non-admin user.
- Add Python 3.9 to PATH. This option will make your life a little bit easier later.
- Customize installation.
- Next.
- Customize install location.
- Change the installation path to
C:\Python39
. - Install.
- 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.
- Hit Win + R.
- Type
cmd
. 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/.
- 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.7, download
GDAL‑3.3.2‑cp39‑cp39‑win_amd64.whl
. Here, cp39 means CPython (the C implementation of Python that we installed above) 3.9. - 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
- Now, install the wheel file:
pip install GDAL‑3.3.2‑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?
- Ctrl + A: to select all the content in a cell or use the mouse to select everything
- Ctrl + C: to copy the selected content into the clipboard
- Open an editor
- Ctrl + V: to paste the copied content
Please try this:
- Start JupyterLab
- Start a new Python 3 Notebook
- Type:
x = 12 print(x)
- Shift + Enter
- That runs, right?
- Click the cell again
- Ctrl + A
- Ctrl + C
- Open an editor
- 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.
- Create a new Python 3 Notebook.
- Type this code in a cell:
name = "FirstLastname" print("Hello " + name)
- Run it by hitting Shift+Enter.
- Take a screenshot of the result and submit it in FirstLastname_JupyterLab.png.