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

998
2
Jump to solution
06-18-2020 10:20 AM
RyanHowell1
New Contributor III

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")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

This could be a quirk with Python scripts in tools, but it could also be that the datetime.datetime class has already been imported somewhere in your Pro session.  If that happens, you will generate this error when trying to use the datetime.datetime default constructor:

>>> from datetime import datetime
>>> datetime.datetime(2000,1,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
>>> 

Two suggestions.  First, close completely out of Pro and then re-launch and run the script tool first before using any other tools or running any other Python interactively.  If that doesn't change the error, try importing datetime and assigning it a different name so the global namespace can't clobber what you are trying to do:

>>> import datetime as dt
>>> dt.datetime(2000,1,1)
datetime.datetime(2000, 1, 1, 0, 0)
>>> 

View solution in original post

2 Replies
JoshuaBixby
MVP Esteemed Contributor

This could be a quirk with Python scripts in tools, but it could also be that the datetime.datetime class has already been imported somewhere in your Pro session.  If that happens, you will generate this error when trying to use the datetime.datetime default constructor:

>>> from datetime import datetime
>>> datetime.datetime(2000,1,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
>>> 

Two suggestions.  First, close completely out of Pro and then re-launch and run the script tool first before using any other tools or running any other Python interactively.  If that doesn't change the error, try importing datetime and assigning it a different name so the global namespace can't clobber what you are trying to do:

>>> import datetime as dt
>>> dt.datetime(2000,1,1)
datetime.datetime(2000, 1, 1, 0, 0)
>>> 
RyanHowell1
New Contributor III

renaming datetime as dt when I imported it seems to have solved the issue. Thanks!

0 Kudos