0

I am running a line of code in a Python 2.7 environment using arcpy.SelectLayerByAttribute_management:

fl_haz_selects = arcpy.SelectLayerByAttribute_management(S_Fld_Haz_Ar, "NEW_SELECTION", "\"DFIRM_ID\" = '{}'".format(fipC))

It runs perfectly fine in Python 3.6. Now I am getting the following errors. What am I doing incorrectly here? Note: S_FLD_Haz_Ar is a shapefile. Do I just need to make it into a layer? I am trying to avoid making a physical layer.

Traceback (most recent call last): File "", line 3, in File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\management.py", line 7713, in SelectLayerByAttribute raise e ExecuteError: Failed to execute. Parameters are not valid. The value cannot be a feature class ERROR 000840: The value is not a Raster Layer. ERROR 000840: The value is not a Mosaic Layer. Failed to execute (SelectLayerByAttribute).

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
gwydion93
  • 1,903
  • 4
  • 28
  • 59

1 Answers1

4

Use MakeFeatureLayer_management to make a layer from your shapefile. (This layer will be saved in memory, so you won't actually be saving a layer file anywhere, and it will be deleted when the application exits.) Then pass that layer to SelectLayerByAttribute_management:

featureLayer = arcpy.MakeFeatureLayer_management(S_Fld_Haz_Ar, "featureLayer")
arcpy.SelectLayerByAttribute_management(featureLayer, "NEW_SELECTION", "\"DFIRM_ID\" = '{}'".format(fipC))

http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/make-feature-layer.htm

Dan Jurgella
  • 2,378
  • 13
  • 15
  • 1
    Note, you can also pass the layer by whatever it was named in the MakeFeatureLayer line. e.g. arcpy.MakeFeatureLayer_management(S_Fld_Haz_Ar, "myLayer") \n arcpy.SelectLayerByAttribute_management("myLayer", "NEW_SELECTION", "\"DFIRM_ID\" = '{}'".format(fipC)) – SMiller Oct 24 '18 at 16:06
  • I am baffled that my old method works fine in 3.6 and 2.7, but I actually came up with a completely easier method that selects the desired records and makes the layer, circumventing the need for the select. By doing fl_haz_selects = arcpy.MakeFeatureLayer_management(S_Fld_Haz_Ar, "fl_haz_lyr", "DFIRM_ID = '{}'".format(fipC)) (I didn't even need to use ""\ in my field name), I was able to make a layer out of only the selected features I want (those with "DFIRM_ID = '{}'".format(fipC))) and eliminate a line of code. – gwydion93 Oct 24 '18 at 19:34
  • For ArcGIS Pro and Python 3.x esri has decided to allow featureclasses to be used as input for SelectLayerByAttribute. http://pro.arcgis.com/en/pro-app/tool-reference/data-management/select-layer-by-attribute.htm – Dan Jurgella Oct 24 '18 at 19:44