arcmap 10.8.1 upgrade broke python scripts

1374
5
Jump to solution
10-26-2021 08:17 AM
MichaelKohler
Occasional Contributor II

updated a machine used for nightly processing from 10.7.1 to 10.8.1. Now I am having issues where I am getting a syntax error on a CalculateField with a function and returns ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 1)

This is the line I have been using for a couple years with no issues.

 

arcpy.management.CalculateField(finalTable, "SiteAddress", "myFun(!LocationStartNumber!,!StreetDirection!,!StreetName!,!StreetWay!,!UnitNumber!)", "PYTHON_9.3", "def myFun(LocationStartNumber,StreetDirection,StreetName,StreetWay,UnitNumber):\\n  if UnitNumber is None:\\n    UnitNumber = ''\\n  if LocationStartNumber == 0:\\n    if StreetName == 'TBD':\\n      return StreetName \\n    else:\\n      val = '{} {} {} {}'.format(StreetDirection,StreetName,StreetWay,UnitNumber)\\n      return val.strip()\\n  else:\\n    return '{} {} {} {} {}'.format(LocationStartNumber,StreetDirection,StreetName,StreetWay,UnitNumber)\\n")

 

I took the calculate out of the script and run it in ArcMap for now. But not a long term solution. What is going on?

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

That code block is terribly difficult to interpret being all on one line like that. Try making a multiline string with three single quotes for the code block.

myFun_def = '''def myFun(LocationStartNumber,StreetDirection,StreetName,StreetWay,UnitNumber):
  if UnitNumber is None:
    UnitNumber = ''
  if LocationStartNumber == 0:
    if StreetName == 'TBD':
      return StreetName
    else:
      val = '{} {} {} {}'.format(StreetDirection,StreetName,StreetWay,UnitNumber)
      return val.strip()
  else:
    return '{} {} {} {} {}'.format(LocationStartNumber,StreetDirection,StreetName,StreetWay,UnitNumber)
'''
arcpy.management.CalculateField(
    in_table=finalTable,
    field="SiteAddress",
    expression="myFun(!LocationStartNumber!,!StreetDirection!,!StreetName!,!StreetWay!,!UnitNumber!)",
    expression_type="PYTHON_9.3",
    code_block=myFun_def
)

 

View solution in original post

5 Replies
BillFox
MVP Frequent Contributor

Do you have ArcGIS Pro installed on the same machine?

0 Kudos
MichaelKohler
Occasional Contributor II

yes. 

However, I use a Task Manager to start the script and explicitly state the path to the python exe I want to use. Also, I have both pro and desktop python environments in my Visual Studio and activate them as needed during development.

0 Kudos
BlakeTerhune
MVP Regular Contributor

That code block is terribly difficult to interpret being all on one line like that. Try making a multiline string with three single quotes for the code block.

myFun_def = '''def myFun(LocationStartNumber,StreetDirection,StreetName,StreetWay,UnitNumber):
  if UnitNumber is None:
    UnitNumber = ''
  if LocationStartNumber == 0:
    if StreetName == 'TBD':
      return StreetName
    else:
      val = '{} {} {} {}'.format(StreetDirection,StreetName,StreetWay,UnitNumber)
      return val.strip()
  else:
    return '{} {} {} {} {}'.format(LocationStartNumber,StreetDirection,StreetName,StreetWay,UnitNumber)
'''
arcpy.management.CalculateField(
    in_table=finalTable,
    field="SiteAddress",
    expression="myFun(!LocationStartNumber!,!StreetDirection!,!StreetName!,!StreetWay!,!UnitNumber!)",
    expression_type="PYTHON_9.3",
    code_block=myFun_def
)

 

MichaelKohler
Occasional Contributor II

I had is formatted that way to reduce the number of lines I had to look at. But I guess the 10.8.1 python didn't like the way I formatted the function.

I had tried it using "" + \  to format but that came out formatted as a single line as well. The triple quotes did the trick. I think I'm going to eventually write files with the functions and read them from there.

Thanks!

0 Kudos
by Anonymous User
Not applicable

I'm curious if its the "PYTHON_9.3" causing the issue.  Maybe try just 'PYTHON3'.

What python are you using? 2.7 or 3?  I think the arcpy.management.CalculateField dot style of syntax is exclusive to 3, and 3 doesn't use PYTHON_9.3 as an expression type parameter.