I am new to Arcpy and have been doing simple tasks such as AddField_management and updateCursor. However I have been getting error msgs, such as errors 99999 etc. The errors I get are related to schema lock or layer not refreshing. So I need to run part of the code, close my mxd and open it again,do this over and over.
I am probably doing something wrong or forgetting something. Per example, I am not setting env.workspace or in_memory or creating temp _lyr files. This is an example of a simple code I am using, from start to finish.
inFeatures = "MyLayer"
fields = ['x','y']
# Add fields to your inFeatures
for field in fields:
arcpy.AddField_management(inFeatures,str(field),"DOUBLE")
with arcpy.da.UpdateCursor(inFeatures, ('x','y',"shape@")) as cursor:
for row in cursor:
row[0] = row[2].firstPoint.X
row[1] = row[2].lastPoint.X
cursor.updateRow(row)
I get the following errors, 'x' was created by 'y' wasnt. I close my mxd and open it again, and I can see field 'y'. Now I run UpdateCursor, nothing changes (still see Null> values). Or I get the error
A column was specified that does not exist
I close the mxd, open it again and I can see the updated columns. This is happening for all my codes, not only this one (ie. not refreshing). My question is, do I need, always, to set my env.workspace? I am running ArcGIS 10.1, files are feature datasets, using Python window in ArcGIS, file path is not long, layers do not have spaces.