I am trying to write a Python script that adds several fields to a feature class and then calculates those new fields. This is a process I will have to do to multiple layers so I am hoping to automate this. I am using ArcGIS Pro 2.4.3 and Python 3.7. I have the following script:
<SPAN class="comment token"># Import system modules</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token"># Set environment settings</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"D:\Project Data\System Analysis\Jan 2020\Sys_analysis_2020\Sys_analysis_2020.gdb"</SPAN>
table <SPAN class="operator token">=</SPAN> <SPAN class="string token">"single_market_area_no_urban_working_intersect"</SPAN>
<SPAN class="comment token"># Add Fields</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"total_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN>
field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"SHORT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"white_only_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN>
field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"SHORT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"no_vehicle_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN>
field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"SHORT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"under18_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN>
field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"SHORT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"over65_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN>
field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"SHORT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"poverty_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN>
field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"SHORT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"total_hh_intersect"</SPAN><SPAN class="punctuation token">,</SPAN>
field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"SHORT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
field_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"minority_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN>
field_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"SHORT"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Calculate newly added fields</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CalculateFields_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN>
expression_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"PYTHON3"</SPAN><SPAN class="punctuation token">,</SPAN>
fields<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"total_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"!total_pop* (!Shape_Area!/!tract_area!)"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"white_only_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"!white_only_pop! * (!Shape_Area!/!tract_area!)"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"no_vehicle_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"!no_vehicle_pop! * (!Shape_Area!/!tract_area!)"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"under18_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'!under18_pop! * (!Shape_Area!/!tract_area!)'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"over65_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"!65over_pop! * (!Shape_Area!/!tract_area!)"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"poverty_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"!poverty_pop! * (!Shape_Area!/!tract_area!)"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"total_hh_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"!total_households! * (!Shape_Area!/!tract_area!)"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"minority_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"!minority_pop! * (!Shape_Area!/!tract_area!)"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>When I run the code above, I receive multiple lines of error messages:
Traceback <SPAN class="punctuation token">(</SPAN>most recent call last<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
File <SPAN class="string token">"<string>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">44</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN>
File <SPAN class="string token">"c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">4265</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> CalculateFields
<SPAN class="keyword token">raise</SPAN> e
File <SPAN class="string token">"c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">4262</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> CalculateFields
retval <SPAN class="operator token">=</SPAN> convertArcObjectToPythonObject<SPAN class="punctuation token">(</SPAN>gp<SPAN class="punctuation token">.</SPAN>CalculateFields_management<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>gp_fixargs<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">,</SPAN> expression_type<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">,</SPAN> code_block<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
File <SPAN class="string token">"c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">506</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN><SPAN class="keyword token">lambda</SPAN><SPAN class="operator token">></SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">lambda</SPAN> <SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">:</SPAN> val<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>gp_fixargs<SPAN class="punctuation token">(</SPAN>args<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcgisscripting<SPAN class="punctuation token">.</SPAN>ExecuteError<SPAN class="punctuation token">:</SPAN> ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Invalid field total_pop
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Invalid field white_only_pop
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Invalid field no_vehicle_pop
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Invalid field under18_pop
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Invalid field 65over_pop
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Invalid field poverty_pop
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Invalid field total_households
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Invalid field minority_pop
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> File <SPAN class="string token">"<expression>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN>
<SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">^</SPAN>
IndentationError<SPAN class="punctuation token">:</SPAN> unexpected indent
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> File <SPAN class="string token">"<expression>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN>
<SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">^</SPAN>
IndentationError<SPAN class="punctuation token">:</SPAN> unexpected indent
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> File <SPAN class="string token">"<expression>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN>
<SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">^</SPAN>
IndentationError<SPAN class="punctuation token">:</SPAN> unexpected indent
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> File <SPAN class="string token">"<expression>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN>
<SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">^</SPAN>
IndentationError<SPAN class="punctuation token">:</SPAN> unexpected indent
ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> File <SPAN class="string token">"<expression>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN>
<SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The error message goes on like that for 1000 lines.
I have been able to run the first half the code by itself and create the new fields with no errors. When I try to run the last half by itself, I receive the same error message as above.
I have tried to just calculate one field using Python and I still receive the same error.
arcpy<SPAN class="punctuation token">.</SPAN>CalculateFields_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN>table<SPAN class="punctuation token">,</SPAN> expression_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"PYTHON3"</SPAN><SPAN class="punctuation token">,</SPAN>
fields<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"total_pop_intersect"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"!total_pop! * (!Shape_Area!/!tract_area!)"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN> Where am I going wrong? Any guidance would be appreciated.