I am developing some code in Python, and I want to use the GDAL/OGR python bindings. What is the minimum I need to install in order to use the GDAL and OGR python bindings?

What is the easiest way to install GDAL/OGR if I only want to use it from Python?
I'm looking for an answer that applies to both mac and windows.

GDAL is listed in the Python Package Index (PyPi) but I don't understand how to install its dependencies (libgdal and the header files for both libgdal and numpy). Maybe all I need to do is install numpy and then get libgdal somewhere, and if so, where? Does a windows install need the Windows Binaries in addition to libgdal and it's header files?

How can I ensure that I get the proper python bindings for my version of Python (2.5, 2.6, 2.7, 3)?
Must changes be made to the PATH environment variable in order to use GDAL and OGR from Python, or can everything be accessed through my site-packages folder and a typical import statement?

I should note that FWTools, OSGeo4W, and kyngchaos offer ogr and gdal with python bindings and are excellent, but FWTools and OSGeo4W ship with their own python, rather than acting as libraries for an existing python installation, and the kyng chaos frameworks appear to be for OS X only.

I'm revisiting this 3 years later. In short, I can see that the problem is that python library installs tend to do a poor job of dealing with dependencies written in C. KyngChaos, FWTools, Anaconda, etc. all do the work of making platform specific C binaries that can be used with the version of Python they have. Basically no one has create a cross platform way to just pip install gdal and have it automatically include and compile all the necessary files from GDAL. It's expected that you will use some other means to create the GDAL binaries.
– BenjaminGolderSep 4 '14 at 22:14

I used Anaconda and it was simple for GDAL, but now I'd like to be able to run ogr2ogr -clipdst -105.53 39.9 -104.93 40.27 places.json ne_10m_admin_0_map_subunits.shp and it seems that I need to enable GEOS from a .configure file - any idea where Anaconda places this file? trac.osgeo.org/geos
– blehmanSep 3 '14 at 16:09

I appreciate the note, but this is similar to the KyngChaos binaries, in that it still doesn't allow you to use an existing python installation, and you must use the version of python shipped with Anaconda.
– BenjaminGolderSep 4 '14 at 22:11

I couldn't get the KyngChaos stuff to work though, hence adding the idea. But Anaconda lets you pick which version of Python to install, so...I don't know, I guess you have a reason for needing the other specific installation to be used?
– mmallekSep 5 '14 at 16:23

1

A Christmas miracle finding this answer some 2+ hours into trying to transfer my (working) set up from PC to Mac.
– ScottieBDec 26 '16 at 22:57

After having to do an environment reset, this no longer works for me. I get an error relating to from osgeo import ogr
– ScottieBFeb 21 '17 at 15:36

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

It is thanks to this answer (reminds me the two 'export' lines that I had forgotten) that I was able to install GDAL in a virtualenv : (venv)mgc@mgc:~$ export CPLUS_INCLUDE_PATH = /usr/include/gdal(venv)mgc@mgc:~$ export C_INCLUDE_PATH = /usr/include/gdal(venv)mgc@mgc:~$ pip3 install gdal==1.11.2 (or the version number corresponding to the gdal library installed on your computer).
– mgcAug 14 '15 at 17:05

I'm not aware of an easy way to install it on OS X, I think Kyng Chaos is the best thing going, as is OSGEO4W on the Windows front. The Python bindings are just wrappers to the actual C/C++ code, you can't get away without installing them either from source or through binaries.

Pip or easy_install can do the trick, depending on the Python version and system libraries present; for example here's the PyPI directory for Python 2.5. Getting GDAL installed consistently across multiple platforms and multiple versions of Python remains non-trivial, but in many ways that makes sense: its perhaps the one library which underlies most open source and many proprietary GIS software, and has a large number of data format dependencies below it.

As regards making sure things don't break, you might want to look at virtualenv and virtualenvwrapper to help keep things tidy.
– Jacques TardieMay 12 '11 at 4:04

PyPI has directories for 2.4 (win32 only, with GDAL 1.5, 1.6), 2.5 (mac/win32 with GDAL 1.5, 1.6) and 2.6 (win32 only, with GDAL 1.6). None have GDAL 1.7. If I understand the gist of the answer correctly, you're mainly saying it's not easy and that pip might work in some cases. Should I be looking into how to build it from source?
– BenjaminGolderMay 12 '11 at 19:36

@BenjaminGolder: right, in my experience it can be tricky, especially if you're trying to support many platforms / and Python versions. Building from source takes more work up front but may be worth the time if you're trying to develop a consistent set of instructions. You may want to also ask your question on the GDAL list which is read by the developers themselves.
– scwMay 15 '11 at 5:43

I'm not the original poster of this answer but this question is locked so have to edit an existing one. This source (ppa:ubuntugis/ppa) is probably less "unstable" than the previous one suggested, and worked out fine for a python2 program I was trying to get to run on my server.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Welcome to GIS@SE, it is better to provide more information with your answer, for example, specific binaries and steps for installation, this makes the information a lot more useful to the person asking the question as well as future people who view the question and answers.
– Mark CupittAug 26 '14 at 1:11

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.