Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to python geospatial programming. i ran the following script and got the corresponding error message

>>> import osgeo
>>> import osgeo.ogr
>>> shapefile = osgeo.ogr.Open("tl_2009_us_state.shp")
>>> numLayers = shapefile.GetLayerCount()

Traceback (most recent call last):   
    File "<pyshell#5>", line 1, in <module>
    numLayers = shapefile.GetLayerCount() AttributeError: 'NoneType' object has no attribute 'GetLayerCount'
share|improve this question
Tried your code locally and it works. So, which version of GDAL do you have installed? – Crischan May 19 '12 at 17:14

So basically, what this is saying, in Python speak, is that your attempt to open the shapefile failed. When something like osgeo.ogr.Open() fails, it usually returns None, which, in your case, gets assigned to your variable "shapefile". When you try to then access shapefile later, it tells you that shapefile is "NoneType" (rather than the type of object that osgeo would have created) and that NoneType objects don't have the method GetLayerCount.

How do you fix this? First, test for errors in your code - it'll give you better messages. Something like:

import osgeo
import osgeo.ogr
    shapefile = osgeo.ogr.Open("tl_2009_us_state.shp")

    if shapefile: # checks to see if shapefile was successfully defined
        numLayers = shapefile.GetLayerCount()
    else: # if it's not successfully defined
        print "Couldn't load shapefile"
except: # Seems redundant, but if an exception is raised in the Open() call,
    #   # you get a message
    print "Exception raised during shapefile loading"

    # if you want to see the full stacktrace - like you are currently getting,
    # then you can add the following:

So, now we need to answer the question of why your shapefile isn't loading. My guess is that you need to provide the fully qualified path (ie, "C:\Users...\tl_2009_us_state.shp") because osgeo can't find your shapefile with the path currently provided. That's a hunch though.

share|improve this answer
No, that's not "Python speak" at all. As Mike says below, instead of returning None, this is what osgeo.ogr ought to do: "IOError [brief description]". – sgillies May 21 '12 at 14:18
Sorry, what I was attempting to say was that "'NoneType' object has no attribute 'GetLayerCount'" is a pretty standard Python error message that often occurs when you expect an object to have been assigned that, for whatever reason, didn't get assigned. Sorry that wasn't clear. – nicksan May 21 '12 at 15:10

@Nick's answer is correct: "NoneType" means that the data source could not be opened. OGR (and GDAL) don't raise exceptions where they normally should, and unfortunately ogr.UseExceptions() doesn't seem to do anything useful. Here is my usual code block to actually raise the proper exceptions:

from osgeo import ogr

# Change this to your OGR data source
ds_fname = r'C:\temp\tl_2009_us_state.shp'

ds = ogr.Open(ds_fname)
if not ds:
    raise IOError("Could not open '%s'"%ds_fname)

numLayers = ds.GetLayerCount()
share|improve this answer

I have encountered this error before and has been stuck with it for so long. I got it to work by using a different shapefile. The US Tiger shapefile must have been corrupted or something. I'm using gdal1.6 here.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.