CalculateField_management. NameError: name is not defined

4607
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
1 Solution

Accepted Solutions
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')‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

20 Replies
DanPatterson_Retired
MVP Emeritus

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

and change to PYTHON_9.3

0 Kudos
MichaelTinker
New Contributor II

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"

0 Kudos
DanPatterson_Retired
MVP Emeritus

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')

MichaelTinker
New Contributor II

Thanks Dan. I tried that earlier!

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

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

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.

Calculate Field—Help | ArcGIS Desktop 

0 Kudos
JoshuaBixby
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')
MichaelTinker
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).

0 Kudos
MichaelTinker
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?

0 Kudos
JoshuaBixby
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?

0 Kudos