AnsweredAssumed Answered

Script tool working fine in python window, but not as a tool?

Question asked by rhowell2 on Jun 18, 2020
Latest reply on Jun 19, 2020 by rhowell2

I have a script tool that populates a "season" field based on the date of the point. For example, if the point is between Nov 1 and Feb 29, the "season" field is "Winter". It also looks to see if a particular individual is in a "nesting" or "brooding" list and, if so, that influences the season type. The tool works fine when I copy and paste the code in to the python window in ArcGIS Pro 2.4, but when run as a script tool, I get the following error:

 

AttributeError: type object 'datetime.datetime' has no attribute 'datetime'. 

 

The error is thrown on line 11 below. Everything looks fine in the output and the print statements as I run it in the python window, as far as I can tell. It's probably something simple as this is the first time I've used the datetime module, but I'm confused why as a tool it won't recognize the datetime.datetime attribute

 

import datetime

full = "name of feature class"

nesting = ['bird1', 'bird2', 'bird3']
brooding = ['bird4', 'bird5', 'bird6']

with arcpy.da.UpdateCursor(full, ['Date', 'Month', 'Year', 'Season', 'SEX', 'NAME']) as cursor:
    for row in cursor:
        if row[3] == None:
            d = datetime.datetime(int(row[2]), int(row[1]), int(row[0]))
            print(d)
            arcpy.AddMessage(d)
            if row[4] == 'MALE':
                if d >= datetime.datetime(int(row[2]), 3, 1) and d <= datetime.datetime(int(row[2]), 6, 15):
                    row[3] = 'Breed'
                    cursor.updateRow(row)
                elif d >= datetime.datetime(int(row[2]), 6, 16) and d <= datetime.datetime(int(row[2]), 8, 31):
                    row[3] = 'Summer'
                    cursor.updateRow(row)
                elif d >= datetime.datetime(int(row[2]), 9, 1) and d <= datetime.datetime(int(row[2]), 10, 31):
                    row[3] = 'Fall'
                    cursor.updateRow(row)
                elif d >= datetime.datetime(int(row[2]), 11, 1) and d <= datetime.datetime(int(row[2]), 12, 31):
                    row[3] = 'Winter'
                    cursor.updateRow(row)
                elif d >= datetime.datetime(int(row[2]), 1, 1) and d <= datetime.datetime(int(row[2]), 2, 29):
                    row[3] = 'Winter'
                    cursor.updateRow(row)
            elif row[4] == 'FEMALE':
                #fall
                if d > datetime.datetime(int(row[2]), 8, 31) and d < datetime.datetime(int(row[2]), 11, 1):
                    row[3] = 'Fall'
                    cursor.updateRow(row)
                #winter
                elif d > datetime.datetime(int(row[2]), 10, 31) and d <= datetime.datetime(int(row[2]), 12, 31):
                    row[3] = 'Winter'
                    cursor.updateRow(row)
                elif d >= datetime.datetime(int(row[2]), 1, 1) and d < datetime.datetime(int(row[2]), 3, 1):
                    row[3] = 'Winter'
                    cursor.updateRow(row)
                #Breed
                elif row[5] not in nesting and row[5] not in brooding and d >= datetime.datetime(int(row[2]), 3, 1) and d <= datetime.datetime(int(row[2]), 6, 15):
                    row[3] = 'Breed'
                    cursor.updateRow(row)
                elif row[5] in nesting and d >=  datetime.datetime(int(row[2]), 4, 10) and d <= datetime.datetime(int(row[2]), 6, 20):
                    row[3] = 'Nest'
                    cursor.updateRow(row)
                elif row[5] in brooding and d >=  datetime.datetime(int(row[2]), 5, 5) and d <= datetime.datetime(int(row[2]), 9, 1):
                    row[3] = 'Brood'
                    cursor.updateRow(row)
                elif row[5] not in brooding and d >=  datetime.datetime(int(row[2]), 6, 16) and d <= datetime.datetime(int(row[2]), 8, 31):
                    row[3] = 'Summer'
                    cursor.updateRow(row)
            else:
                print('assign point manually')
        else:
            print('Type already calculated')
        cursor.updateRow(row)
        print('done')
arcpy.AddMessage("Complete")

Outcomes