ArcGIS Pro 2.9 - Python Script Failing for Labels

2735
18
11-22-2021 08:05 AM
PeteJordan
Occasional Contributor III

Before upgrading to 2.9, I never had an issue with my labeling code at all:

 

def FindLabel([TrailName],[Width]):

  str_list = []
  if [TrailName] not in (None, "<NULL>","<Null>", "", "0"):
    str_list.append([TrailName])
    
  if [Width] not in (None, "<NULL>","<Null>", "", "0", " "):
    width_str = '<ITA>Width = {}\'</ITA>'.format("<ITA>" + [Width] + "</ITA>")
    str_list.append(width_str)    

  label_str = '\n'.join(str_list)
  return(label_str)

 

 

  Now that I switched to 2.9, I'm now getting the following error:

Invalid Expression

Traceback (most recent call last):

   File "<expression", line 1, in <module>

NameError: name 'FindLabel' is not defined

  I have pasted the exact same code into ArcMap 10.8.1 and it works just fine.  So I'm very confused why all of a sudden a labeling script I've used for over a year now just fails in Pro?

  Any ideas?

 

 

 

 

18 Replies
JoeBorgione
MVP Emeritus

Did you recently upgrade to Pro 2.9 form an earlier version of Pro or is this a fresh install?  Did this work in Pro 2.8.x and earlier?

That should just about do it....
PeteJordan
Occasional Contributor III

@JoeBorgione:  Yes I was on 2.8.3 before and the label script has worked just fine in all previous Pro versions for the past year or more.  In fact I used the script the week before without any issues.  

 

0 Kudos
DuncanHornby
MVP Notable Contributor

I believe the issue lies with this line:

width_str = '<ITA>Width = {}\'</ITA>'.format("<ITA>" + [Width] + "</ITA>")

Replace it with this:

width_str ="<ITA>Width = " + [Width] + "</ITA>"

 

I can only guess some changes behind the scene have occurred, may be with the parser or version of python?

I think the writing is on the wall with python as they are pushing Arcade as the default, cross platform labelling language, so it might be worth your while investing some time learning this new scripting language.

PeteJordan
Occasional Contributor III

@DuncanHornby I tried your suggestion, but, no luck and I continue to get the same issue that it can't define the "FindLabel" name that I defined in the first line.

  Just seems really strange.  Yeah I think it might take me way too long to learn Arcade, I'm not the quickest at learning languages at all...

0 Kudos
DuncanHornby
MVP Notable Contributor

Pete,

Not sure why you are still getting an error as the code works for me, I suspect you don't have the correct indentation?

I spoofed up your fields in a dataset I have and as you can see it works:

DuncanHornby_0-1637666225537.png

The code being:

def FindLabel([TrailName],[Width]):

  str_list = []
  if [TrailName] not in (None, "<NULL>","<Null>", "", "0"):
    str_list.append([TrailName])
    
  if [Width] not in (None, "<NULL>","<Null>", "", "0", " "):
    width_str ="<ITA>Width = " + [Width] + "</ITA>"
    str_list.append(width_str)    

  label_str = '\n'.join(str_list)
  return(label_str)
0 Kudos
PeteJordan
Occasional Contributor III

Yeah I have no idea.  I've always just copied the code and it's always worked until 2.9.  As I rolled back to 2.8.3 due to other issues I've found in 2.9, the same code works as it should in 2.8.  I don't get it at all.  I had even copied the code directly from 2.9 and tested it in ArcMap and it worked there as well.  So I have no idea other than some freak glitch with the 2.9 install or something?

DuncanHornby
MVP Notable Contributor

Yeah I've had a bit of a nightmare with 2.9. Not a single tool would run! I had to uninstall everything, kill off some user directories and then reinstall from an ISO I had downloaded. But now fingers crossed it does appear to be stable and my processing is chugging alone fine...

I got quite excited about the new toolbox format until this... 😉

0 Kudos
PeteJordan
Occasional Contributor III

Good to know then it's not just me who has had issues.  I can't even get my keybinds to save anymore and for editing in Pro, that's a nightmare without that ability.  I might just way for the next 2.9 patch then and hopefully that fixes some things, but good to know it wasn't just me that's having 2.9 issues at all...

JoeBorgione
MVP Emeritus

Since it worked in earlier versions of Pro, this sounds like a new undocumented feature (aka bug) at 2.9.  In the old days of ArcInfo as well as ArcGIS the unwritten rule was never update to a new version that ends in dot zero.  I relearned this lesson at 2.8.0 and rolled back to 2.7.4. Once 2.8.2 came out I updated to that.  If this was happening to me, I'd roll back to the last version your script worked.

That should just about do it....