<?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 Re: Select by Attribute fails using Python Layer Object in Standalone script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200184#M15380</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What version are you running?&amp;nbsp; I am running 10.5, and I just tried to recreate your problem using an MXD and I could not.&amp;nbsp; I am not sure if I didn't set everything up the same as you or if something else is going on.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does you map layer have a definition query? What type of data store is the layer in, file geodatabase?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 31 May 2017 18:47:10 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2017-05-31T18:47:10Z</dc:date>
    <item>
      <title>Select by Attribute fails using Python Layer Object in Standalone script</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200181#M15377</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How a script can work within a map document python interpreter but fail as a standalone script has puzzled me several times and I usually find a way to work around it.&amp;nbsp; This one though I want answers to.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have simplified a part of a much larger script to indicate the issue I am having, all I want to do is select all the features within the current map extent(I need a count of them, so I use this with GetCount).&amp;nbsp; Since Select By Attribute works with the Extent Environmental variable, I figured that was the easiest way to do it.&amp;nbsp; When running with the python interpreter in a map document, this script works fine.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
mxd &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;mapping&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;MapDocument&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"CURRENT"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
Layer2 &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;mapping&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListLayers&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;mxd&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"USA City Populations"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
df &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;mapping&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListDataFrames&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;mxd&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;extent &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; df&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;extent
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SelectLayerByAttribute_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;Layer2&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It select&amp;nbsp;all the features in my dataframe extent as I want.&amp;nbsp; However, I want to run this as a standalone, only changing the mxd to the file path of the map document instead of the "CURRENT" key word it fails.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ExecuteError: ERROR 000358: Invalid expression&lt;BR /&gt;Failed to execute (SelectLayerByAttribute).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Select By Attribute is supposed to be able to work with TOC Layers, so I don't see why there is an issue using a Layer Object.&amp;nbsp; I've also attempted this with the Layer Name as it appears in the TOC hard-coded in as the input for SelectLayerByAttribute.&amp;nbsp; Again this works fine when run within the map document with the python interpreter and using the "CURRENT" Keyword but fails as a standalone with the map document path hard coded in.&amp;nbsp; I know the workaround is just to make a Feature Layer, but I want to understand why this simple code won't work standalone but will within the python interpreter.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:58:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200181#M15377</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2021-12-11T09:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select by Attribute fails using Python Layer Object in Standalone script</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200182#M15378</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;if it is totally offline, I am not sure why you would want to work with a dataframe.&amp;nbsp; In any event, an in arcmap can become a *.lyr for use out of arcmap, it references the data and contains the symbology&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 May 2017 16:10:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200182#M15378</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-05-31T16:10:35Z</dc:date>
    </item>
    <item>
      <title>Re: Select by Attribute fails using Python Layer Object in Standalone script</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200183#M15379</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is part of a larger script that will eventually export the map to a pdf.&amp;nbsp; I need to get the count of the features in the current map extent for that layer then change the definition query depending on the count.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 May 2017 18:34:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200183#M15379</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2017-05-31T18:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: Select by Attribute fails using Python Layer Object in Standalone script</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200184#M15380</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What version are you running?&amp;nbsp; I am running 10.5, and I just tried to recreate your problem using an MXD and I could not.&amp;nbsp; I am not sure if I didn't set everything up the same as you or if something else is going on.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does you map layer have a definition query? What type of data store is the layer in, file geodatabase?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 May 2017 18:47:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200184#M15380</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2017-05-31T18:47:10Z</dc:date>
    </item>
    <item>
      <title>Re: Select by Attribute fails using Python Layer Object in Standalone script</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200185#M15381</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I figured it out, problem with a bad definition query.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 May 2017 19:27:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attribute-fails-using-python-layer/m-p/200185#M15381</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2017-05-31T19:27:53Z</dc:date>
    </item>
  </channel>
</rss>

