[GIS] I’ve downloaded the GDAL-1.11.1 package but why can’t I call on it in python

gdalmacpython

I have a Mac and am very new to this coding stuff..

I'm using a code that includes

import osgeo as gdal

and so I've downloaded GDAL from https://pypi.python.org/pypi/GDAL/ which has a folder called "osgeo" and a python script called "gdal", but when I run the whole code I get the message "ImportError: No module named 'osgeo'"

Can someone give me the babysteps about how to import this correctly?

Best Answer

Your code is wrong. Do this:

from osgeo import gdal

GDAL is a module of the osgeo package. You don't ever import osgeo itself. In Python a package is a convenient collection of modules and provides a 'name-space' for that collection. So, you can drill down further and import specific functions or variables from a module within a package like this:

from aPackage import aModule.someFunction

The statement from osgeo import gdal fetches everything in the gdal module. Sometimes you only want parts of a module and it can be more efficient to just import what you need. Don't worry about that for now. In your case, the three most common imports you will do when using GDAl are

from osgeo import gdal
from osgeo import ogr
from osgeo import osr

GDAL gives you raster functionality. OGR gives you vector functionality and OSR gives you spatial reference functionality. Have a look at this excellent if old tutorial. It will help you get started.

Related Question