# Error 000728 field does not exist within table - field already exists

I am running arcpy.AddField_management to add a field called "cnid" and update it with x centroid values. For add.field I get the error "cnid" already exists. I tried print.field name and I cannot see "cnid" field. Now I am running calculate shape centroid and I get the error message "cnid" does not exist within table. I cannot understand, sometimes it works (if I close arcmap and open it again) but if I run both codes, add field and update it, it doesnt work.

    # AddField for new fields
arcpy.AddField_management("Connections","cnid","DOUBLE", "", "", "", "", "NULLABLE")

#test fields
for field in arcpy.ListFields ("Connections"):
...     print field.name

xExpression = "float(!SHAPE.CENTROID!.split()[0])"
inFeatures ="Connections"
fieldName ="cnid"
arcpy.CalculateField_management ("Connections","cnid",xExpression, "PYTHON")


I read something about in_memory and refresh bug from Why am I getting an ERROR 000728: Field *fieldname* does not exist within table even though field exists?, but how can I refresh it without having to close and open it again?

I am using ArcMap's Python window.

• It looks like "Connections" is a layer, perhaps it would be better to add the field to the table the layer is from and remake the layer... possibly "in_memory\\connections". That part of the code isn't there. – Michael Stimson Dec 15 '14 at 3:05
• "Connections" is feature class, I have just closed and open it again and "cnid" is there now. >:/ Where should I include "in_memory\\connections" in my code? thanks – JrUser Dec 15 '14 at 3:19
• Is your arcpy.env.workspace = "in_memory"? Normally feature classes are given by their full path unless you're set to the workspace that they're in, layers (not layer files) don't have a path which is where it's a bit confusing. – Michael Stimson Dec 15 '14 at 3:28
• Would you be able to edit your question to include all of a code snippet that you can run to see things working up to where you are stuck, and then the whole error message that is being returned when you run that code snippet, please? Also, let us know where you are running this code from e.g. IDLE, ArcMap's Python window, a Python script tool, or ...? – PolyGeo Dec 15 '14 at 3:43
• When responding to requests for clarifications would you be able to use the edit button to revise your question body rather than contributing to a longer comment trail, please? How to ask a good question? is a useful reference on how best to attract potential answerers to your question. – PolyGeo Dec 15 '14 at 4:58

!table_name.field_name!