PROBLEM

When using Calculate Field in Python to update a text field, a 'value is not defined' message causes the script to fail

Last Published: April 25, 2020

Description

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.

For example:

Code:
Runtime error <class 'arcgisscripting.ExecuteError'>:
ERROR 000539: Error running expression:TEXT <type 'exceptions.NameError'>:
name 'TEXT' is not defined Failed to execute (CalculateField).

Cause

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.

Solution or Workaround

The following is an example of the correct syntax for arcpy.CalculateField_management to update a field with a string value.

When using arcpy.CalculateField_management, expressions that contain strings must be formatted as follows:

"'TEXT'"

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.

Code:
arcpy.CalculateField_management("FeatureClass", "FieldName", "'Hello_World'", "PYTHON_9.3")

Article ID:000011652

Software:
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic