Select to view content in your preferred language

Problem Setting Label Expression in Arcpy

4443
3
Jump to solution
12-24-2013 03:53 AM
JonathanMulder
Occasional Contributor
I'm trying to set a label expression to a field ("StateWellNumber") in my table.  I have the following code but I get an error message saying "name 'StateWellNumber' is not defined".

Any ideas?

Jon Mulder

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 =  [StateWellNumber] 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaChisholm
Frequent Contributor
Sorry I should have caught that.

lyr.labelClasses is an array (list) of multiple labelClass items. Changes are it is a array of 1 item(s).

The '.expression' variable can be used on a single labelClass (not an array or 'list' of labelClasses). You have two (at least) choices:

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.
lyr.labelClasses[0].expression =  "[StateWellNumber]"


Two; you can apply the expression to all labelClasses in the layer. You'll need to remove the line lyr.labelClasses.expression =  "[StateWellNumber]" and add a for loop.
for labelClass in lyr.labelClasses:     labelClass.expression = "[StateWellNumber]"


Hope that helps!!

View solution in original post

0 Kudos
3 Replies
JoshuaChisholm
Frequent Contributor
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.

I'd also suggest (not functionally nessesary, but it'll look better) simplifying your script a little by defining a layer variable, like this:
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 =  "[StateWellNumber]"

Good luck!
~Josh
0 Kudos
JonathanMulder
Occasional Contributor
Thanks Josh,

Still fairly new to this, so wasn't really aware of defining a layer variable.  I incorporated your suggestion about quotes, but now get the following error:

AttributeError: 'list' object has no attribute 'expression'.  I tried it without the brackets and get the same error.


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 =  "[StateWellNumber]"
0 Kudos
JoshuaChisholm
Frequent Contributor
Sorry I should have caught that.

lyr.labelClasses is an array (list) of multiple labelClass items. Changes are it is a array of 1 item(s).

The '.expression' variable can be used on a single labelClass (not an array or 'list' of labelClasses). You have two (at least) choices:

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.
lyr.labelClasses[0].expression =  "[StateWellNumber]"


Two; you can apply the expression to all labelClasses in the layer. You'll need to remove the line lyr.labelClasses.expression =  "[StateWellNumber]" and add a for loop.
for labelClass in lyr.labelClasses:     labelClass.expression = "[StateWellNumber]"


Hope that helps!!
0 Kudos