CalculateField_management. NameError: name is not defined

4648
20
Jump to solution
05-11-2017 08:15 PM
MichaelTinker
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?

Thanks much for your help!

Best regards, Mike

0 Kudos
20 Replies
MichaelTinker
New Contributor II

No, it doesn't. However, it is a parameter input by the user.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Then you need to show the show the script where the parameter is getting passed.  And it is probably a string parameter, so check to see if you need to 'float' it

0 Kudos
curtvprice
MVP Esteemed Contributor

No need to float it, a parameter "{}".format("0.1") is the same as "{}".format(0.1). The tricky part is to get your user-input variable into the string m.

0 Kudos
DanPatterson_Retired
MVP Emeritus

.... show the show the script where the parameter is getting passed .... my comment

This is the part that is missing... getparameterastext? getparameter? if it was just 0.01, then everything should work as designed

MichaelTinker
New Contributor II

Gentlemen, thanks for your feedback. Been offline couple of days.

Here are the relevant lines of the "real" script in question

slope = arcpy.GetParameterAsText(2)
m = float(slope)
arcpy.CalculateField_management(NHDFlowline,'THRESHOLD', '!MEAN! * m', 'PYTHON')

Where NHDFlowline is an existing table in an output gdb (set as the working environment). A couple fields I have added to this table are THRESHOLD and MEAN.

I am attempting to field calculate THRESHOLD by multiplying (MEAN * m) where m is the slope of a regression line.

0 Kudos
curtvprice
MVP Esteemed Contributor

This error means you got your parameters out of order. Joshua's first example should work well if you can figure out how to plug in your workflow. Feel free to post a code snippet (formatted, please): https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?s...

0 Kudos
DanPatterson_Retired
MVP Emeritus

Here are 4 examples using one of my calculations...

#  formatting the expression
m = 0.1
expr = "!POINT_Y! * {}".format(m)
expr   # yields
'!POINT_Y! * 0.1'


# putting the 0.1 within the expression
arcpy.CalculateField_management(in_table="fishnet_label",
                                field="test",
                                expression="!POINT_Y! * 0.1",
                                expression_type="PYTHON_9.3",
                                code_block="")

# put the expression in from above
arcpy.CalculateField_management(in_table="fishnet_label",
                                field="test",
                                expression=expr,
                                expression_type="PYTHON_9.3",
                                code_block="")

# write a code block
codeblk ='m =  0.1\nglobal m\ndef test(fld):\n    return fld * m')
arcpy.CalculateField_management(in_table="fishnet_label",
                                field="test",
                                expression="test(!POINT_Y!)",
                                expression_type="PYTHON_9.3",
                                code_block=codeblk)

Something has to work

MichaelTinker
New Contributor II

Thanks for sticking with the thread Dan. I'll go through examples.

NHDFlowline = 'D:\Generalizer\output.gdb\NHDFlowline_0202'
HUC = 'D:\Generalizer\output.gdb\huc12_mn_ppt_0202'
arcpy.CalculateField_management(NHDFlowline,'THRESHOLD','!MEAN! * .01', 'PYTHON')

This first one works. However, m is supposed to be an input parameter so...

m = input("enter m")
NHDFlowline = 'D:\Generalizer\output.gdb\NHDFlowline_0202'
HUC = 'D:\Generalizer\output.gdb\huc12_mn_ppt_0202'
arcpy.CalculateField_management(NHDFlowline,'THRESHOLD','!MEAN! * m', 'PYTHON')

This is the usual error I get, which is: NameError: name 'm' is not defined.

Ok, lets use your first example:

m = input("enter m")
expr = '!MEAN!' * {}.format(m)
NHDFlowline = 'D:\Generalizer\output.gdb\NHDFlowline_0202'
HUC = 'D:\Generalizer\output.gdb\huc12_mn_ppt_0202'
arcpy.CalculateField_management(NHDFlowline,'THRESHOLD','expr', 'PYTHON')

Now the error says: 

expr = '!MEAN!' * {}.format(m)
AttributeError: 'dict' object has no attribute 'format'

It thinks that !MEAN! is a dictionary object...

m = input("enter m")
expr = '!MEAN!' * {}.format(m)
NHDFlowline = 'D:\Generalizer\output.gdb\NHDFlowline_0202'
HUC = 'D:\Generalizer\output.gdb\huc12_mn_ppt_0202'
arcpy.CalculateField_management(in_table=NHDFlowline,field='THRESHOLD',expression='expr',expression_type='PYTHON',code_block="")

This one has the same error: expr = '!MEAN!' * {}.format(m)
AttributeError: 'dict' object has no attribute 'format'

0 Kudos
curtvprice
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')‍‍‍‍‍‍‍‍‍‍‍
MichaelTinker
New Contributor II

Thanks Curtis, that did it!

0 Kudos