I'm using Arcmap 10.1, running python 2.7 on a Windows 8.1 machine. I have a script that uses the arcpy.CalculateField_management function to calculate the area of a shape, however, for some reason when i use the python expression to calculate the field, python crashes. The only way i can use the arcpy.CalculateField_management function is by using a VB expression. Does anyone one how i can translate this line to VB

arcpy.CalculateField_management("TAZ_un_Buffer_Dissolve1.shp", "totalArea","!shape.area!", "PYTHON", "")

Have in mind that the line below returns an error. 'arcpy.CalculateField_management("TAZ_un_Buffer_Dissolve1.shp", "totalArea","[shape.area]", "VB", "")'

How else can i accomplish this without using the arcpy.CalculateField_management function?

  • This may sound like a dumb question, but are you sure you even have a shape.area field in your shapefiles? ShapeArea fields are generally geodatabase managed fields, but aren't managed fields in SHP files (so it would only be there if you created it or it was included during an export from a GDB). Also, what field type is the "totalArea" field? – John Apr 17 '14 at 16:36
  • The filed is a double. I have been having an issue with the arcpy.CalculateField_management in where like i said i can only calculate using a VB expression – Daniel Apr 17 '14 at 17:27
  • 1
    Could you please provide more information to help us identify the source of your crash ? Does your script run without the calculateField function ? Does your script run if you don't use the shape field in the arcpy.CalculateField_management (e.g. arcpy.CalculateField_management("TAZ_un_Buffer_Dissolve1.shp", "totalArea","1", "PYTHON", "") ? Does Python work in the field calculator with this shapefile when you are in ArcMap ? Does the field calculator with "calculate geometry" run correctly on this shapefile ? Maybe the problem comes from your shapefile and not from Python – radouxju Apr 18 '14 at 5:38

The documentation only makes mention of being able to use shape.area on Python expressions. You will need to use a Python expression to do this.

I would very strongly recommend reinstalling ArcGIS. Python not working is indicative of a corrupt installation of ArcGIS, and working around it will not alleviate the problem in other areas.

  • Yeah i think ill have to re install everything. Could it be that i also have Anaconda installed that is python2.7 to crash? – Daniel Apr 17 '14 at 17:29
  • I doubt it (I've gotten arcpy working in an Anaconda environment without issues), but I have no idea what on your system may be causing the error. Architecture issues (x64 DLLs in the path shadowing x86 DLLs), file copy issues on install, faulty third-party Python packages, etc. – Jason Scheirer Apr 17 '14 at 17:40

Maybe you should start with a "repair geometry" and check your coordinate system to make sure that your shapefile is not the source of your problem.

If calculateField still doesn't work, but the rest of your Python script runs properly, probably the best solution would be to use an arcpy.da.updateCursor() and compute the area for each row.

Possible workaround would be to use "calculate area" or to export your data in a geodatabase. In both cases you end up with a new feature class that would have a field with the area of your polygons (but it creates a new data).

If none of those work, then you should reinstall.

NOTE : In older (<10) versions, you would have needed a codeblock expression to return the area in VBA, but VBA is now deprecated. A vbscript version should look like this but it is not working anymore with ArcObjects as variables since ArcGIS 10.1:

expression = "dblArea"
codeblock = """Dim dblArea
Dim pArea ' as IArea in VBA
Set pArea = [shape]
dblArea = pArea.area"""
arcpy.CalculateField_management("TAZ_un_Buffer_Dissolve1.shp", "totalArea","expression", "VB", "codeblock")
  • 1
    You can't put [field] references in a code block. Also, the poster states that Python isn't working at all on their install, so how could they call a VB expression from a Python script? – Jason Scheirer Apr 17 '14 at 20:17
  • 1
    You cannot directly use field name in Python codeblocks, but with VBscript this is how it works (see help.arcgis.com/fr/arcgisdesktop/10.0/help/index.html#//…) . Also, not everything with python is failing for the OP, because the script uses arcpy. From my understanding, his script fails when he uses PYTHON inside his calculate field management). If the OP was not working with a Pyton script, he would simply use "calculate geometry" on his column. – radouxju Apr 17 '14 at 20:40

Thanks for all the help but I think I fixed the problem. The computer had two versions of python 27 installed and seems that that is the reason behind python crashing when using the CalcFiled method. I removed the version Arcgis does not use and it is not crashing anymore so its working normal now. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.