4

I found a useful field calculator code block that I'm trying to add to a custom python tool. It works great in the manual field calculator; however, when inserted into the code block text to be used as a script, I get "NameError: name 'fc' is not defined". Apparently the script isn't seeing my variable. My question then is: How do I get the variable inside the code block?

I've tried everything I can think of including but not limited to setting 'fc' as global, escaping 'fc' from the code block string, and defining 'rows' outside of the code block, but I seem to be getting nowhere. I feel like I'm missing something obvious, but I'm out of ideas for today. Below is my code and the error:

import arcpy

fc=arcpy.GetParameterAsText(0)

DupNumCode="""uniquelist = {}
rows = arcpy.SearchCursor(fc)
for row in rows:
  value = row.getValue('SpatialID')
  if value not in uniquelist:
    uniquelist[value] = 1
  else:
    uniquelist[value] = uniquelist[value] + 1

def findIncidence(inValue):  
  return uniquelist[inValue]"""

DupNumExpression="findIncidence(!SpatialID!)"

arcpy.CalculateField_management(fc, "DupNum", DupNumExpression, "PYTHON", DupNumCode)

I get the following:

ExecuteError: ERROR 000539: Runtime error 
Traceback (most recent call last):
  File "<string>", line 2, in <module>
NameError: name 'fc' is not defined

Failed to execute (CalculateField).

Here is my parameter setup:

enter image description here

  • 1
    Can you post a screenshot of how you're defining parameters in the python script tool (from the toolbox)? – Jvhowube Oct 11 '16 at 18:24
  • 1
    You can edit your post and after saving your image to a file upload it into the post as a picture. – Richard Fairhurst Oct 11 '16 at 18:27
  • 1
    Welcome to GIS SE! To edit your question, click Edit under your question (or in this comment). – Midavalo Oct 11 '16 at 18:28
2

Your fc is not definied within the code block. You can pass it to the code block using string format:

DupNumCode = """uniquelist = dict()
fc = {0}
rows = arcpy.SearchCursor(fc)
for row in rows:
  value = row.getValue('SpatialID')
  if value not in uniquelist:
    uniquelist[value] = 1
  else:
    uniquelist[value] = uniquelist[value] + 1

def findIncidence(inValue):  
  return uniquelist[inValue]""".format(fc)

Note that I have changed uniquelist = {} to be uniquelist = dict() to ensure the format doesn't try to insert into the {} here.

  • You've got unbalanced quotes in the first line of code – Vince Oct 11 '16 at 18:53
  • @Vince unsure what you mean here? Triple quotes open the code block, triple quotes close the code block. – Midavalo Oct 11 '16 at 18:55
  • 1
    Doh. I see it now. But I expect the {0} will cause trouble. Sometimes it's more readable to append individual lines as strings. – Vince Oct 11 '16 at 19:00
  • It's the combination of {} and {0} within a format that's scary. – Vince Oct 11 '16 at 20:34
  • I have updated it to remove the ambiguity – Midavalo Oct 11 '16 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.