Using GDAL with Python, pip and Windows 10

Sasha Patsel
2 min readApr 20, 2020

--

Intro

GDAL is a library built to process geospatial data. It is incredibly useful, but can often be tricky to get working on whatever system you’re using. This guide is for developers who want to set up GDAL in a python virtual environment using pip.

Prerequisites

- Python >= 3.7.4

- pip >= 20.0.2

Installing GDAL

* Make sure you have created a virtual environment and have activated it before starting the next steps.

You will likely be unable to install GDAL directly via pip on Windows 10.

Instead, download a .whl file for the library, and install using that file. Select the desired version here.

Once you’ve downloaded the .whl file, go ahead and add it to your environment. The command to install is `pip install <path_to_whl_file>`.

Setting up your environmental variables

If you’ve tried to get GDAL to work in a Python environment before, it’s possible you’ve installed an instance in a local environment, globally, or both. You may have installed it everywhere and it still doesn’t work.

In our opinion, it is easiest (and cleanest) to import GDAL from a virtual environment. It allows you to have the highest degree of control over what you’re using.

You also don’t want a “hybrid” situation where you’re importing GDAL from your local environment, but your environmental variables point to a global instance.

So once you’ve installed GDAL in your virtual environment using pip, you’ll want to declare your environmental variables as follows (where `BASE_DIR` is a string which points to the directory where your virtual environment can be found):

os.environ['PATH'] = os.path.join(BASE_DIR, r'venv\Lib\site-packages\osgeo') + ';' + os.environ['PATH']os.environ['PROJ_LIB'] = os.path.join(BASE_DIR, r'env3\Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']GDAL_LIBRARY_PATH = os.path.join(BASE_DIR, r'venv\lib\site-packages\osgeo\gdal300.dll')

The installation via the .whl file and the environmental variables should be all you need to get you going. What we’ve done here is made sure that we’ve installed GDAL only within our virtual environment, and we’ve indicated to python where to find the files it needs in order to get the library to work.

Happy Hacking!

--

--