<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic [SOLVED] Re: Select layer by attribute does not work in standalone script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-does-not-work-in/m-p/300400#M23235</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That did the trick, thanks mzcoyle &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I had this thought in my head that I first needed to have an empty layer before using add_to_selection to step by step increase the number of features in the layer. But apparently, the layer I created with makefeaturelayer had no selection on it, just a definition query.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 25 Jan 2013 07:20:05 GMT</pubDate>
    <dc:creator>MennoNijhuis2</dc:creator>
    <dc:date>2013-01-25T07:20:05Z</dc:date>
    <item>
      <title>Select layer by attribute does not work in standalone script, does in arcmap</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-does-not-work-in/m-p/300398#M23233</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm having trouble with the SelectLayerByAttribute_management() tool. Although it produces no errors or whatsoever, I get an empty selection.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the code I am using in my standalone script. I've run it down to the part that's causing trouble and added lots of checks &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy database = 'D:/Menno/openhabitat/Keillour/HabitatSDM/Habitats.gdb' fc = database + '/SurveyData/HabitatSiteArea' subs = ['504S34','504S35'] sql = """ "PolygonNo" = '%s' """ lyr = 'lyr'&amp;nbsp; # create empty layer to add features to arcpy.MakeFeatureLayer_management(fc,lyr,sql % '') print 'layer exists:', arcpy.Exists(lyr) print 'features in lyr:', arcpy.GetCount_management('lyr').getOutput(0) print 'featureclass exists:', arcpy.Exists(fc) print 'features in feature class:', arcpy.GetCount_management(fc).getOutput(0) l = arcpy.ListFields(fc) m = [] for a in l: &amp;nbsp;&amp;nbsp;&amp;nbsp; m = m + [a.name] print 'field exists in featureclass:', 'PolygonNo' in m&amp;nbsp; # loop along PolygonNos and add each feature to the selection layer for f in subs: &amp;nbsp;&amp;nbsp;&amp;nbsp; print 'PolygonNo:', f &amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Sql:', sql % f &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr,'ADD_TO_SELECTION', sql % f) &amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Features in lyr after select:', arcpy.GetCount_management(lyr).getOutput(0) ##arcpy.DeleteFeatures_management(lyr) print 'End'&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This evaluates to:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;layer exists: True features in lyr: 0 featureclass exists: True features in feature class: 60 field exists in featureclass: True PolygonNo: 504S34 Sql:&amp;nbsp; "PolygonNo" = '504S34'&amp;nbsp; Features in lyr after select: 0 PolygonNo: 504S35 Sql:&amp;nbsp; "PolygonNo" = '504S35'&amp;nbsp; Features in lyr after select: 0 End&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As you can see, layer exists, feature class exists, field exists, there are features in the featureclass, and on my word, there are also features with the mentioned attributes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Performing a select by attributes from the arcmap menu or arcmap attribute table view, using the exact same sql phrases does result in a selection. Copying the lines one by one to the arccatalog or arcmap script window does not.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Jan 2013 12:54:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-does-not-work-in/m-p/300398#M23233</guid>
      <dc:creator>MennoNijhuis2</dc:creator>
      <dc:date>2013-01-23T12:54:26Z</dc:date>
    </item>
    <item>
      <title>Re: Select layer by attribute does not work in standalone script, does in arcmap</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-does-not-work-in/m-p/300399#M23234</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is your problem here. You have a layer with no features in it because of your query. Remove the query.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.MakeFeatureLayer_management(fc,lyr,sql % '')&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Jan 2013 13:03:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-does-not-work-in/m-p/300399#M23234</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-01-23T13:03:57Z</dc:date>
    </item>
    <item>
      <title>[SOLVED] Re: Select layer by attribute does not work in standalone script</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-does-not-work-in/m-p/300400#M23235</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That did the trick, thanks mzcoyle &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I had this thought in my head that I first needed to have an empty layer before using add_to_selection to step by step increase the number of features in the layer. But apparently, the layer I created with makefeaturelayer had no selection on it, just a definition query.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 07:20:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-does-not-work-in/m-p/300400#M23235</guid>
      <dc:creator>MennoNijhuis2</dc:creator>
      <dc:date>2013-01-25T07:20:05Z</dc:date>
    </item>
  </channel>
</rss>

