# CalculateField_management. NameError: name is not defined

4086
20
05-11-2017 08:15 PM
New Contributor II

Hello,

Why am I getting a NameError here?

Here is the basic python:

table = r'D:\output.gdb\table'
m = 0.01
arcpy.CalculateField_management(table, 'FIELD_B', '!FIELD_A! * m', 'PYTHON')

The error states: NameError: name 'm' is not defined

How is this so, as I have just defined m right above?

Best regards, Mike

1 Solution

Accepted Solutions
MVP Esteemed Contributor

Code block #3 would have worked -- but you need a quote moved, and don't quote expr!  (It's a python variable of type str, not a string literal.)

``````m = input("enter m")
expr = '!MEAN! * {}'.format(m)  # quote entire expression
# if m is .01, expr now is: "!MEAN! * .01"
NHDFlowline = 'D:\Generalizer\output.gdb\NHDFlowline_0202'
HUC = 'D:\Generalizer\output.gdb\huc12_mn_ppt_0202'
arcpy.CalculateField_management(NHDFlowline, 'THRESHOLD', expr, 'PYTHON')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
20 Replies
MVP Legendary Contributor

not sure but try ... '!FIELD_A!' * m

and change to PYTHON_9.3

New Contributor II

If I try:

arcpy.CalculateField_management(table, 'FIELD_B', '!FIELD_A! ' * m, 'PYTHON 9.3')

The error is different. It states:

"can't multiply sequence by non-int type of float"

MVP Legendary Contributor

so the multiply by m  doesn't work because it is interpreting '!MEAN!' as a string which you can't multiply by a float.

The only other thing I can think of is to put the expression out separately.

m = 0.01

expr = '!MEAN! * {}'.format(m)

which prints out

'!MEAN! * 0.1'

then .... (fltable, field='THRESHOLD', expression=expr, 'PYTHON_9.3')

New Contributor II

Thanks Dan. I tried that earlier!

The result is: "SyntaxError: non-keyword arg after keyword arg"

MVP Esteemed Contributor

I don't know if thus will make a difference, and not head a machine where I can test, but all the samples show double quote not single around the field and expression.

MVP Esteemed Contributor

In Python, you can't pass positional arguments after keyword arguments.  With Dan's example, try using all positional:

``````table = r'D:\output.gdb\table'
m = 0.01
expr = '!FIELD_A! * {}'.format(m)
arcpy.CalculateField_management(table, 'FIELD_B', expr, 'PYTHON_9.3')‍‍‍‍``````

or using all keywords once you start using keywords:

``````table = r'D:\output.gdb\table'
m = 0.01
expr = '!FIELD_A! * {}'.format(m)
arcpy.CalculateField_management(table,
field='FIELD_B',
expression=expr,
expression_type='PYTHON_9.3')‍‍‍‍‍‍‍``````
New Contributor II

Joshua, thank you. However, the error now says: Parameters are not valid.
ERROR 000800: The value is not a member of VB | PYTHON | PYTHON_9.3.
Failed to execute (CalculateField).

New Contributor II

Somehow, the variable m is not being passed. CalculateField doesn't seem to know anything about the variable m.  So...how to pass it?

MVP Esteemed Contributor

If the examples above from Dan and myself, the variable m is not being passed, the variable it getting converted to a value, 0.01 in this case, before being passed to the field calculator.  Does the value of m vary by record?