I have a python script that used to work to calculate difference in dates outputting the result in days. It worked fine since last week until we migrated to 10.4. To my understanding, this should not matter but just as en extra info.
(datetime.datetime.now() - arcpy.time.ParseDateTimeString( !DATE! )).days
The following error is returned:
Executing: CalculateField gishw.PU.MapChangeRequest Time_Diff (datetime.datetime.now() - arcpy.time.ParseDateTimeString( !DATE! )).days PYTHON_9.3 #
Start Time: Wed Apr 27 10:24:54 2016
ERROR 000539: Error running expression: (datetime.datetime.now() - arcpy.time.ParseDateTimeString( u"4/26/2016 4:10:00 PM" )).days
Traceback (most recent call last):
File "<expression>", line 1, in <module>
NameError: name 'arcpy' is not defined
Failed to execute (CalculateField).
Failed at Wed Apr 27 10:24:54 2016 (Elapsed Time: 0.13 seconds)
Any assistance will be appreciated.
you have to import arcpy, particularly if you are working in a python ide outside of arcmap
add this line to code
if it errors out, then the installation isn't correct
Yes, I am working on a python IDE. When executed from the IDE, it says in runs but the record is not updated in the Time_Diff field. That is why I went to the field calculator to just test the script which was working last week. This is a simple script that used to update one of the widgets in a dashboard.
# Import arcpy module
conn = r"C:\Users\gispu\Documents\GIS_Python_Scripts\Map_Change_Request\GISHW.sde"
arcpy.env.workspace = conn
# Local variables:
Input_MapChangeRequest = r"C:\Users\gispu\Documents\GIS_Python_Scripts\Map_Change_Request\GISHW.sde\gishw.PU.HW_FieldCollection\gishw.PU.MapChangeRequest"
MapChangeRequest_Layer = "PU.MapChangeRequest_Layer"
MapChangeRequest_Layer2 = MapChangeRequest_Layer
PU_MapChangeRequest_Layer = MapChangeRequest_Layer2
# Process: Make Feature Layer
# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(MapChangeRequest_Layer, "NEW_SELECTION", "STATUS = 'Open' OR STATUS = 'Validation'")
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(conn)
# Edit session is started without an undo/redo stack for versioned data
# Start an edit operation
# Process: Calculate Field
arcpy.CalculateField_management(Input_MapChangeRequest, "Time_Diff", "(datetime.datetime.now() - arcpy.time.ParseDateTimeString( !DATE! )).days", "PYTHON_9.3", "")
# Stop the edit operation.
# Stop the edit session and save the changes
The above script is good and works as expected.
Did you import arcpy into your Field Calculator? I see it in your stand alone script but I am wondering about the field calculator portion. Or is the exact script above the same you used in Field Calculator?
No, the arcpy module was not imported when using the Field Calculator. If needed, can you provide an example of how to? BTW, I found it odd that it is needed since I already tested it before within the Field Calculator wand I don't recall adding the import module part.
Just add it to the top of the code block scripting area like you would in a standalone script. Here are some examples:
arcpy.env.workspace = "C:/data"
arcpy.AddField_management("vegtable.dbf", "VEG_TYP2", "TEXT", "", "", "20")
arcpy.CalculateField_management("vegtable.dbf", "VEG_TYP2", '!VEG_TYPE!.split(" ")[-1]', "PYTHON_9.3")
EDIT: I have not tested this. I am just kind of going off of the documentation.
If you are using the field calculator and not the calculate field tool, there should be no need to import arcpy. Also I see that sde is involved... this is about the 12 post this week with issues of calculating stuff in files that reside or are destined to sde. I wonder if it is just the week, or some bigger issue going on with the explanations in the documentation. Try working local, store remotely in the short term.
Thanks for the info... I was just checking out your blog
I will try to run the same process with a File GDB in order to isolate the issue. If it does work, I will report it here for the record.
Issue solved. The script was working but for some reason the field storing the time difference got renamed with an underscore at the end. After removing it, the script is working as expected. The datetime module was not required since it is included as part of the python functions within the Field Calculator GUI.
I really appreciate your assistance Dan.