I understand that in order to do a SelectByAttribute in Python, you need to first convert the shapefile to a feature layer using the MakeFeatureLayer_management function, and run the SelectByAttribute on that feature layer. My question is, is there a way to show your selection on the original shapefile? I don't want to create a new output and add it to the map document. I just want to run the script and have the selection shown on the map.

What I have. I want the selection to be shown on my FIELD shapefile.

shape = "FIELD"    
arcpy.MakeFeatureLayer_management(shape, "shape_lyr")    
arcpy.SelectLayerByAttribute_management("shape_lyr","NEW_SELECTION",""""TYPE" = 'HIGH'""")
  • this might help you in creating a python toolbox as you requested
    – dassouki
    Dec 5, 2014 at 15:42

1 Answer 1


If you are in ArcMap, you should just be able to run your SelectLayerByAttribute() by referencing the layer as it is in the TOC, and your selection should show up. See picture below

My python window:

>>> arcpy.SelectLayerByAttribute_management("Parcels", "NEW_SELECTION", """OWNER_NAME like '%SMITH%'""")
<Result 'Parcels'>

enter image description here

To set your where clause parameter, just set the Data Type to "SQL Expression" and in the parameter properties you need to set the "Obtained from" property to the layer the user chooses.

enter image description here

To get the layer list you'll need to use the Tool Validator to list the layers in the mxd and set the list like this:

mxd = arcpy.mapping.MapDocument('current') 
self.params[i].filter.list = arcpy.mapping.ListLayers(mxd) 

where i would would be the parameter index.

  • Thank you. Is there a way to do this in a stand-alone script? I have a Toolbox with script tools...
    – Steven C
    Dec 4, 2014 at 21:06
  • Yes, but if you are using a stand alone script the map has to be closed. You have to reference the layer by name using the lyr = arcpy.mapping.ListLayers(mxd, 'layer name')[0]. Once you have referenced the layer, you can run the select by attribute and then save the map using mxd.save(). Next time you open it, there should be a selection.
    – crmackey
    Dec 4, 2014 at 21:09
  • So there is no way to run that script from the Toolbox in an open map, and have the selection shown on the map?
    – Steven C
    Dec 4, 2014 at 21:12
  • There is, you can just set the mxd variable to be the current map like this: mxd = arcpy.mapping.MapDocument('current'). You could also set up validation to list the layers in the mxd as a drop down then have a where clause parameter to give you the Select By Attribute GUI, which would be obtained from the layer you choose.
    – crmackey
    Dec 4, 2014 at 21:52

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.