Python – Using OGR and GDAL Outside OSGEO4W Shell

ogrosgeo4wpython

I have QGIS installed, therefore it installed the OSGeo4W with all functionalities and that works great.

I added all the environment variables as proposed in this post: Is there a way to use gdal functions from OSGeo4W out of their shell?

But what I'm looking for is to actually be able to use my normal Python shell outside OSGeo4W and :

import ogr

This is since I'm using Apache Spark to make some analysis I don't want to have two different Python environments.

Best Answer

If you are looking for pure python with gdal on your cmd you need to do follow steps:

Step 1: Install pure python from python.org

Feel free to download the latest 2.7x version of python (rather than the 3.x python version). Install python with the default options and directories.

Step 2: Next Install the GDAL Binaries

Head over to Tamas Szekeres’ Windows binaries and download the appropriate GDAL Binary.

Install gdal core and next install gdal bindings for your version python.

Step 3: Adding Path Variables:

  1. Right click on “Computer” on the desktop and go to “Properties”

  2. Click on Advanced System Properties

  3. Select Environment Variables.

  4. Under the System variables pane, find the ‘Path’ variable, then click on Edit.

  5. Go to the end of the box and copy and paste the following: ;C:\Program Files (x86)\GDAL Note: For 64-bit GDAL installations you would simply remove the (x86) after Program Files.

  6. In the same System variables pane, click on “New” and then add the following in the dialogue box:

    Variable name: GDAL_DATA

    Variable value: C:\Program Files (x86)\GDAL\gdal-data

  7. Click “OK”

  8. Add one more new variable by clicking “New…”

  9. Add the following in the dialogue box:

    Variable name: GDAL_DRIVER_PATH

    Variable value: C:\Program Files (x86)\GDAL\gdalplugins

For testing GDAL install open command line and type: gdalinfo --version