I previously successfully used the sample code here: How to calculate Field using part of filename in ModelBuilder?

# Import standard library modules
import arcpy, os, sys
from arcpy import env

# Allow for file overwrite
arcpy.env.overwriteOutput = True

# Set the workspace directory 
env.workspace = r"C:\temp.gdb" 

# Get the list of the featureclasses to process
fc_tables = arcpy.ListFeatureClasses()

# Loop through each file and perform the processing
for fc in fc_tables:
    print str("processing " + fc)

    # Define field name and expression
    field = "SID"
    expression = str(fc[:5]) #subsets first 5 characters of fc name

    # Create a new field with a new name

    # Calculate field here
    arcpy.CalculateField_management(fc, field, "expression", "PYTHON") 

But now I'm running it again, with no changes, even with the same datasets I used before, and it no longer works. I cannot see how or why it errors out.

Error message:

Runtime error : ERROR 000539: Error running expression: expression : name 'expression' is not defined Failed to execute (CalculateField).


2 Answers 2


Instead of:

expression = str(fc[:5])


expression = '"' + str(fc[:5]) + '"'


  • The contents of expression is evaluated by a second Python interpreter within the Calculate Field tool. Thus anything you want to be interpreted as a string literal needs to be enclosed with quotes.
  • +1 for an explanation! I never really understood that. Makes sense.
    – bluefoot
    Commented Mar 28, 2013 at 17:34

Take the quotes completely off in the calculate field function:

arcpy.CalculateField_management(fc, field, expression, "PYTHON")

It's a variable, not a string. It stores the string you already set when doing

expression = str(fc[:5])

See example 2 in the ArcGIS Help docs and you'll see how they set an expression to a variable and passes it to the Calculate Field function. No quotes.



arcpy.CalculateField_management(fc, field, "expression", "PYTHON")

should be

arcpy.CalculateField_management(fc, field, '"' + expression + '"', "PYTHON")

This will add double quotes around your string, which should work out for you.

  • Thanks for the quick response! Unfortunately then I get this error: Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: ten_m <type 'exceptions.NameError'>: name 'ten_m' is not defined Failed to execute (CalculateField).
    – J Louie
    Commented Mar 28, 2013 at 17:22
  • so basically it took the filename for the first feature class and is saying it's not defined.
    – J Louie
    Commented Mar 28, 2013 at 17:25
  • This is not correct in this situation. Calculate Field has its own Python interpreter and that interpreter will evaluate the contents of the expression argument, which is assigned by the outer interpreter. So by that time, str(fc[:5]) has already been evaluated into a string. If you do not wrap that string in quotes, it will be interpreted by the inner interpreter as an identifier, not a string literal. That is why the quotes are necessary.
    – blah238
    Commented Mar 28, 2013 at 17:58
  • @blah238 Thank you so much!!! This did the trick! I am still absolutely baffled as to why the original code worked, a number of times, quite literally last week! @Mike Just to clarify, I DID have to remove the quotes from arcpy.CalculateField_management(fc, field, expression, "PYTHON") When I left the quotes around "expression", it gave me the same undefined error as I originally posted.
    – user16616
    Commented Mar 28, 2013 at 18:04
  • Ah good point. I went back and looked at the syntax in the help menu forgot that the expression still needs to be wrapped in quotes using the Python parser, regardless if it's a string or not.
    – Mike
    Commented Mar 28, 2013 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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