<?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: Problem Setting Label Expression in Arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407796#M32111</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Josh,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Still fairly new to this, so wasn't really aware of defining a layer variable.&amp;nbsp; I incorporated your suggestion about quotes, but now get the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;AttributeError: 'list' object has no attribute 'expression'.&amp;nbsp; I tried it without the brackets and get the same error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;LayerFileLocation = "H:\Documents\GIS\HydstraData"
LayerName = "WellPoints.lyr"
##Define layer:
lyr = arcpy.mapping.Layer(os.path.join(LayerFileLocation,LayerName))
##Show labels for WellPoints.
lyr.supports("LABELCLASSES")
lyr.showLabels = True
lyr.labelClasses.expression =&amp;nbsp; "[StateWellNumber]"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 18:32:45 GMT</pubDate>
    <dc:creator>JonathanMulder</dc:creator>
    <dc:date>2021-12-11T18:32:45Z</dc:date>
    <item>
      <title>Problem Setting Label Expression in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407794#M32109</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm trying to set a label expression to a field ("StateWellNumber") in my table.&amp;nbsp; I have the following code but I get an error message saying "name 'StateWellNumber' is not defined".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jon Mulder&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;LayerFileLocation = "H:\Documents\GIS\HydstraData" LayerName = "WellPoints.lyr" ##Show labels for WellPoints. arcpy.mapping.Layer(os.path.join(LayerFileLocation,LayerName)).supports("LABELCLASSES") arcpy.mapping.Layer(os.path.join(LayerFileLocation,LayerName)).showLabels = True arcpy.mapping.Layer(os.path.join(LayerFileLocation,LayerName)).labelClasses.expression =&amp;nbsp; [StateWellNumber] &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Dec 2013 11:53:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407794#M32109</guid>
      <dc:creator>JonathanMulder</dc:creator>
      <dc:date>2013-12-24T11:53:33Z</dc:date>
    </item>
    <item>
      <title>Re: Problem Setting Label Expression in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407795#M32110</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It should be as simple as putting [StateWellNumber] in quotes. This is because you need to pass a string to the expression. Right now python is looking for a variable (defined in the script) called StateWellNumber and can't find it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'd also suggest (not functionally nessesary, but it'll look better) simplifying your script a little by defining a layer variable, like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;LayerFileLocation = "H:\Documents\GIS\HydstraData"
LayerName = "WellPoints.lyr"
##Define layer:
lyr=arcpy.mapping.Layer(os.path.join(LayerFileLocation,LayerName))
##Show labels for WellPoints.
lyr.supports("LABELCLASSES")
lyr.showLabels = True
lyr.labelClasses.expression =&amp;nbsp; "[StateWellNumber]"&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Good luck!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;~Josh&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:32:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407795#M32110</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-11T18:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: Problem Setting Label Expression in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407796#M32111</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Josh,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Still fairly new to this, so wasn't really aware of defining a layer variable.&amp;nbsp; I incorporated your suggestion about quotes, but now get the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;AttributeError: 'list' object has no attribute 'expression'.&amp;nbsp; I tried it without the brackets and get the same error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;LayerFileLocation = "H:\Documents\GIS\HydstraData"
LayerName = "WellPoints.lyr"
##Define layer:
lyr = arcpy.mapping.Layer(os.path.join(LayerFileLocation,LayerName))
##Show labels for WellPoints.
lyr.supports("LABELCLASSES")
lyr.showLabels = True
lyr.labelClasses.expression =&amp;nbsp; "[StateWellNumber]"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:32:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407796#M32111</guid>
      <dc:creator>JonathanMulder</dc:creator>
      <dc:date>2021-12-11T18:32:45Z</dc:date>
    </item>
    <item>
      <title>Re: Problem Setting Label Expression in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407797#M32112</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Sorry I should have caught that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr.labelClasses is an array (list) of multiple labelClass items. Changes are it is a array of 1 item(s). &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The '.expression' variable can be used on a single labelClass (not an array or 'list' of labelClasses). You have two (at least) choices:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One; you can assume your layer only has one labelClass (this is likely the case) and apply the expression to the first one. Keep in mind that arrays start at 0 not 1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;lyr.labelClasses[0].expression =&amp;nbsp; "[StateWellNumber]"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Two; you can apply the expression to all labelClasses in the layer. You'll need to remove the line &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;lyr.labelClasses.expression =&amp;nbsp; "[StateWellNumber]"&lt;/SPAN&gt;&lt;SPAN&gt; and add a for loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;for labelClass in lyr.labelClasses: &amp;nbsp;&amp;nbsp;&amp;nbsp; labelClass.expression = "[StateWellNumber]"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Dec 2013 13:25:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-setting-label-expression-in-arcpy/m-p/407797#M32112</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2013-12-24T13:25:59Z</dc:date>
    </item>
  </channel>
</rss>

