Not sure that this will resolve all the issues, but try the following code?
import arcpy
def main():
#Set inputs
feature = ''
field = ''
sum_field = ''
#Get the sum
total_val = sum([row[0] for row in arcpy.da.SearchCursor(feature, [field])])
#Calculate the value
table = arcpy.MakeTableView_management(feature, 'table')
arcpy.CalculateField_management(table, sum_field, total_val, 'PYTHON_9.3')
arcpy.Delete_management(table)
return
if __name__ == '__main__':
main()
The float() conversion isn't necessary since python will do this automatically if it sees float data.Alternatively, if you save the following with a .pyt extension, ArcGIS will automatically recognize it as a toolbox:
import arcpy
class Toolbox(object):
def __init__(self):
self.label = 'Calculate Sum'
self.alias = ''
# List of tool classes associated with this toolbox
self.tools = [CalculateSum]
class CalculateSum(object):
def __init__(self):
self.label = 'Calculate Sum'
self.description = ''
self.canRunInBackground = True
def getParameterInfo(self):
p0 = arcpy.Parameter(
name = "input_file",
displayName = "Input File",
datatype = "DEFeatureClass",
parameterType = "Required",
direction = "Input")
p1 = arcpy.Parameter(
name = "value_field",
displayName = "Select Value Field",
datatype = "Field",
parameterType = "Required",
direction = "Input")
p1.parameterDependencies = [p0.name]
p2 = arcpy.Parameter(
name = "sum_field",
displayName = "Select Field to Populate",
datatype = "Field",
parameterType = "Required",
direction = "Input")
p2.parameterDependencies = [p0.name]
params = [p0, p1, p2]
return params
def isLicensed(self):
'''Set whether tool is licensed to execute.'''
return True
def updateParameters(self, parameters):
'''Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed.'''
return
def updateMessages(self, parameters):
'''Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation.'''
#Make it impossible for the two fields to be the same.
if parameters[1].valueAsText == parameters[2].valueAsText:
parameters[1].setErrorMessage("Value field cannot be the same as Populate Field.")
parameters[2].setErrorMessage("Populate Field value cannot be the same as Value field.")
return
#Run the functions for this tool.
def execute(self, params, messages):
#Get the parameters
feature, field, sum_field = [p.valueAsText for p in params]
#Get the sum
total_val = sum([row[0] for row in arcpy.da.SearchCursor(feature, [field])])
#Calculate the value
table = arcpy.MakeTableView_management(feature, 'table')
arcpy.CalculateField_management(table, sum_field, total_val, 'PYTHON_9.3')
arcpy.Delete_management(table)
return
Hope this helps. Let me know if it doesn't work for you.