I have a shapefile "selected_polygons.shp" that contains 2000 polygons. I would like to select each polygon contained in the shapefile by using a search cursor in order to apply the function "CostDistance" for each polygon.

Here is my code:

import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\Users\LCP"
arcpy.env.extent = "costs.tif"

with arcpy.da.SearchCursor("selected_polygons.shp",("*")) as rowsInPatches_start:

for rowStart in rowsInPatches_start:        

 ID_patch_start = rowStart.getValue("GRIDCODE") 

 ## Define SQL expression for the fonction Select Layer By Attribute
 expressionForSelectInPatches_start = "GRIDCODE=%s" % (ID_patch_start) 

 # Process: Select Layer By Attribute in Patches_start
 arcpy.MakeFeatureLayer_management("selected_patches.shp", "Selected_patch_start", expressionForSelectInPatches_start)

 # Process: Cost Distance
 outCostDist=CostDistance("Selected_patch_start", "costs.tif", "", "outCostLink.tif")

 # Save the output 

The problem is that I obtain a error message when I use:

with arcpy.da.SearchCursor("selected_polygons.shp",("*")) as rowsInPatches_start:

Here is my error message:

 AttributeError: 'module' object has no attribute 'da'
  • Just verifying that you are in fact using 10.1 and not 10? da only works upwards of 10.1 – GISKid Aug 15 '14 at 14:29
  • No problem, it was just the wrong version? – GISKid Aug 15 '14 at 15:28
  • Yes, It was just the wrong version. – Marine Aug 15 '14 at 15:34
  • If you could mark my answer below as the solution for future users with the same problem, that'd be great! – GISKid Aug 15 '14 at 15:40

The da module only works in versions upwards of 10.1 in ArcGIS. If you try to run a python code using da in ArcGIS 10 or below, you would get the error seen below:

AttributeError: 'module' object has no attribute 'da'

If you run the code in the correct version it should resolve itself. If you only have access to 10, then use the regular arcpy.searchcursor and adapt your code. Penn state has a good resource for using search cursors and the differences between 10 and 10.1 .

3.2.2 Reading through records

  • GISKid,i have the same problem and i work with arcview 10.3, what can i do? – newGIS Jan 27 '15 at 11:33
  • @Y.Y.C Maybe post another question with your code! – GISKid Jan 28 '15 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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