Installing GDAL for Python in Windows -Aayush Panda
By: Aayush Panda
Tags: Gdal, Coding, GIS, Programming, Python

Stuck behind a paywall? Click here to view the article for free using my friend link

Introduction

GDAL (Geospatial Data Abstraction Library) is a library for manipulating raster and vector geospatial data. From Google Maps to QGIS, GDAL is perhaps the most commonly used software in modern GIS applications. As a result of its compatibility with Python, it is also the go-to library for any geospatial data analysis.

Unfortunately, GDAL is notorious for being difficult to set up, and there are few resources available on the internet that show the process of installation. This makes using GDAL difficult for many hobbyists, developers and GIS professionals alike. This article will walk you through step-by-step in installing GDAL with Python binders in Windows 10, along with a guide for installation scenarios with multiple Python installations.

Note that for some users, it is advisable to run multiple environments instead of installing GDAL to their main Python installation.

Installing Python 3.8

As of now, GDAL only supports Python 3.8+, and that is the version of Python we will be using throughout this guide. If you have another version of Python already installed, there is a section dealing with multiple Python installations.

Download Python 3.8 from https://www.python.org/downloads/. I recommend that you install it in C:/Program Files, but that is not a mandate.

For users with multiple Python installations:

If you already have a Python installation, you can install Python 3.8 as well. This will give you multiple Python installations on your system, and you will have to take some additional steps to ensure that GDAL is able to locate Python 3.8 packages. To do this, you will have to change the hierarchy of the system variables for each Python installation. Note that this will change your “default” Python interpreter, so you might have to change this again if you are planning to use another version of Python for a long term project.

First open the Start menu, type Path , and click Enter to select Edit environment variables for your account . This will open the System Properties window. From here, click Environment Variables to open the Environment Variables window. Open the Path variable in the System Variables menu. You will find a window like this:

Note how Python 3.7 values are “above” Python 3.8 values

Since Python 3.7 values are above Python 3.8 values, all programs will use Python 3.7 as the default Python interpreter. This can cause problems with GDAL, which is only compatible with Python 3.8 +. To fix this issue, we only have to move the Python 3.8 values above other Python values as such:

Python 3.8 is now the default Python interpreter

Once again, remember that this will change your default python interpreter. If you intend to work on a project that utilises another Python installation, you will have to change the value hierarchy to better suit your needs.

Installing prerequisite libraries

A prerequisite library for GDAL is NumPy, a Python library dealing with multidimensional matrices and arrays with a large collection of high-level mathematical functions. While installing NumPy, the library is not installed at the user site packages folder. This will not allow GDAL to discover it, and many functionalities will fail to work.

You can install NumPy using PIP, either from the command line or from your IDE. If you are installing NumPy from your IDE, ensure that it is running as administrator and that “Install to user site packages” is disabled.

If you are using CMD to install NumPy, ensure that it too is running as administrator, and install NumPy using: pip install numpy

For users with multiple Python installations:

If you have already followed the instructions in the “Installing Python 3.8” section, running any Python commands from CMD should default to Python 3.8. If you face any problems despite this, or if you did not change your system variables, you can follow these methods:

It might be difficult trying to specify which version of Python you want the packages to be installed to. If you are using an IDE like PyCharm, you can set the project interpreter to Python 3.8, and that should allow you to install NumPy to Python 3.8 .

If you are using CMD to install NumPy, you can circumvent any issues by running PIP from the desired Python executable. You can do this by moving to your Python 3.8 directory using cd C:/Program Files/Python38 and then installing NumPy using python.exe -m pip install numpy .

Downloading the GDAL wheel and Python binders

Now that we have the prerequisites for GDAL set up, it is time to install GDAL itself. Download the wheel from: lfd.uci.edu/~gohlke/pythonlibs/#gdal

If you are using Python 3.8 (recommended), download GDAL‑3.0.4‑cp38‑cp38‑win_amd64.whl . Once downloaded, install it from CMD. Once again, ensure that the terminal is running as administrator and that correct version of Python is being used (using the methods described in the previous section). To install the wheel, use the following command:

pip install download-dir/GDAL‑3.0.4‑cp38‑cp38‑win_amd64.whl

Setting up environment

Note that you might have to change the directories to suit your Python installation location.

Add to system path variable:

Open the system variable window, and add

C:\Program Files\Python38\Lib\site-packages\osgeo

to the system path variable.

Create system variables:

1. Variable Name: GDAL_DATA

Variable Value: C:\Program Files\Python38\Lib\site-packages\osgeo\data\gdal



2. Variable Name PROJ_LIB

Variable Value: C:\Program Files\Python38\Lib\site-packages\osgeo\data\proj

Adding GeoDataBase API

Download “File Geodatabase API 1.4 version for Windows (Visual Studio 2013)” from appsforms.esri.com/products/download/index.cfm . Unzip the downloaded zip file. Copy only FileGDBAPI.dll to

C:\Program Files\Python38\Lib\site-packages\osgeo\

Setting GDAL_DRIVER_PATH variable

Find C:\Program Files\Python38\Lib\site-packages\osgeo\__init__.py and uncomment the line that is as shown:

# uncomment the next line to enable plugins

os.environ[‘GDAL_DRIVER_PATH’] = os.path.join(os.path.dirname(__file__), ‘gdalplugins’)

Finishing up