In a stand-alone Python IDE or ArcMap's Python window, when attempting to update a field with a string value using arcpy.CalculateField_management, the error message: name 'TEXT' is not defined; 'Failed to execute (Calculate Field)' may occur.
Runtime error <class 'arcgisscripting.ExecuteError'>:
ERROR 000539: Error running expression:TEXT <type 'exceptions.NameError'>:
name 'TEXT' is not defined Failed to execute (CalculateField).
The cause for this error message is related to the way that Python reads string values. Typically, when entering a string in a Python script, double quotation marks are used around the string, as in: "TEXT".
When using the Python parser in ArcMap's Field Calculator, a string is defined by using single quotes: 'TEXT'.
Using arcpy.CalculateField_management in a script requires the use of the same formatting used by the Field Calculator Python parser (that is, single quotes) in addition to the Python script formatting (double quotes). Keep this in mind when making a variable to pass into this tool as well.
When using arcpy.CalculateField_management, expressions that contain strings must be formatted as follows:
The structure is: double quote, single quote, text, single quote, double quote.
This allows the Python parser to recognize the string as defined, as shown in the code example below.
arcpy.CalculateField_management("FeatureClass", "FieldName", "'Hello_World'", "PYTHON_9.3")