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).

  • 3
    You need to create a layer from the shapefile before doing selection. – ahmadhanb Oct 24 '18 at 15:01

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))


| improve this answer | |
  • 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. pro.arcgis.com/en/pro-app/tool-reference/data-management/… – Dan Jurgella Oct 24 '18 at 19:44

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