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?
Thanks much for your help!
Best regards, Mike
Solved! Go to Solution.
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')
not sure but try ... '!FIELD_A!' * m
and change to PYTHON_9.3
Thanks for your response.
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"
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')
Thanks Dan. I tried that earlier!
The result is: "SyntaxError: non-keyword arg after keyword arg"
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.
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')
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).
Somehow, the variable m is not being passed. CalculateField doesn't seem to know anything about the variable m. So...how to pass it?
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?