|

How to Install GDAL for Python with pip on Windows

Installing GDAL for Python with pip , the package installation package, is not as easy as it may seem. If you try to install GDAL to a python environment with a command like python -m pip install gdal it will initially seem like the installation is going to happen. However, you will receive a message that pip failed to build the python wheel file, then a long, verbose error message. Not to worry. You can still install gdal with pip, and here’s how to do it. You can also check out the video at the end of this article for a demonstration. Once you get GDAL installed, check out our tutorial to get started using GDAL with Python. You can also check out my full GDAL for Python course to get step-by-step explanations complete with code and datasets for performing raster analysis with GDAL.

If you want to install gdal to a conda (Anaconda) environment you can follow my tutorial for installing gdal with conda. Anaconda users can also follow this tutorial but the conda method is recommended.

First, let’s understand why the problem occurs. GDAL is actually a C++ library with python bindings. That means it relies on underlying C++ code and the package must be built/compiled in a certain manner to be usable with Python. GDAL is an open-source project, and depending on the user group and other applications there may not be resources to provide a python-specific distribution or other considerations that prevent it. The upside is that because it is open-source anyone can develop a solution, and I’ll explain one now.

1. Download a pre-built gdal wheel file

With pip we can use a wheel file to install a package. Wheel files contain all the information needed to install a python package. Often when pip is used for install it downloads a wheel file for the install. A gdal wheel file can be obtained from Christoph Gohlke’s website. You’ll need to select the file that matches your specific Python version and operating system (32 or 64 bit). Once you find the appropriate file download it to a location you remember

2. Install the wheel file with pip

Now all you have to do is install the wheel file with pip. I recommend navigating the directory where you downloaded the file (with cd) before running the command below. Alternatively, you can stay in your current directory and pass the full path of the wheel file.

python -m pip install path-to-wheel-file.whl

Assuming that you’ve used the appropriate wheel file, this should successfully install gdal

3. Test the installation

Now make sure you can load gdal in a python session. From the command prompt type python to start an interactive session. Once the session has started you will see a description of your python version followed by >>> indicating that the next code you type will be interpreted by python.

Type from osgeo import gdal and hit Enter. If nothing happens then gdal was successfully imported. If you get an error message something obviously went wrong. You may also want to try import gdal, especially for older python versions.

Conclusion

Hopefully, this guide has helped you get gdal running for Python. Check out our guide to getting started with GDAL to make the most of your new installation. If you’re still having issues you may want to try creating a new, clean environment for your gdal installation. If you’re not using an Anaconda Python distribution you may want to try that and use the conda guide to install gdal.

Want to learn more about GDAL?

I have a full-length course on using GDAL for raster analysis with Python. The course contains six hours of video instruction and is complete with code and example datasets so you can easily follow along and replicate the concepts that are taught. Check it out at geospatialschool.com!

Whether you’re looking to take your GIS skills to the next level, or just getting started with GIS, we have a course for you! We’re constantly creating and curating more courses to help you improve your geospatial skills.

All of our courses are taught by industry professionals and include step-by-step video instruction so you don’t get lost in YouTube videos and blog posts, downloadable data so you can reproduce everything the instructor does, and code you can copy so you can avoid repetitive typing


My Recommended Equipment

Computer: Dell XPS

Mouse: Logitech M557 Bluetooth Mouse

External Hard Drive: Seagate Portable 2TB


This article contains affiliate links. When you click on links in this article Open Source Options may make a commission on any sales. This does not have any impact on the price you pay for products.

Similar Posts