# Complicated expression for Calculate Field for arcpy

I have a working formula that I can put in the Field Calculator which I'd like to use in my python code:

.3-.001*Log ( [Field1]+1 )-.02*Log ( [Field2]+1 )-.04*Log ( [Field3]+1 )

My code:

``````import math
expression =".3-.001*Log(!Field1!+1)-.02*Log(!Field2!+1)-.04*Log(!Field3!+1)"
arcpy.CalculateField_management("Layer","Field4",expression)
``````

ExecuteError: ERROR 999999: Error executing function. Syntax error Failed to execute (CalculateField).

How can I write this expression?

• `expression =".3-.001*math.log(!Field1!+1)-.02*math.log(!Field2!+1)-.04*math.log(!Field3!+1)"` – Bjorn Nov 20 '17 at 18:51

You should change "Log" to "math.log" .

If you want to use your code in Field Calculator, just use formula in it:

0.3-.001*math.log(!Field1!+1)-0.02*math.log(!Field2!+1)-0.04*math.log(!Field3!+1)

In Python IDE:

You need to assign "PYTHON_9.3" to the last parameter of the "arcpy.CalculateField_management".

Don't forget import arcpy in the beginning of the code when you use the code in the Python IDE or console.

`````` import arcpy,math
expression = '.3-.001*math.log(!Field1!+1)-.02*math.log(!Field2!+1)-.04*math.log(!Field3!+1)'
arcpy.CalculateField_management("Layer","Field4",expression,"PYTHON_9.3")
``````
• ive always wondered why you need import arcpy for field calculator... its inside arcmap you would think its imported by default (like the arcmap python terminal?) – NULL.Dude Nov 20 '17 at 20:14
• I supposed you use this code in the python IDE. OK i edited my answer. If you use this code in the field calculator you'll not need to use import arcpy – BBG_GIS Nov 20 '17 at 20:16
• @Geo.Dude, @wetland If you're using this code in the field calculator you wouldn't be calling `arcpy.CalculateField_management()` – Midavalo Nov 25 '17 at 4:11
• @Midavalo You'r right. I edited the answer. Thanks. – BBG_GIS Nov 25 '17 at 4:54