Why does DLT modify datetime import definition

890
2
Jump to solution
10-06-2020 01:04 PM
DETCOG_GIS
New Contributor II

The ExecuteDataLoad tool, when run in python at least, appears to overwrite the datetime module imported by import arcpy.  This sample code hopefully describes the issue:

import arcpy‍

import datetime
#import datetime appears to be part of import arcpy
#but for readability of code, you can include it, though it's redundant

#even if we exclude "import datetime", the following line shows it imports the module
print(datetime)
#returns <module 'datetime' from 'C:\\...\\ESRI\\conda\\envs\\arcgispro-py3-clone\\lib\\datetime.py'>

#so you can do things like the following:
print(datetime.datetime.now())

#Now lets run the data loading tools
arcpy.dlt.ExecuteDataLoad(r'...\dataloadingworkspace\DataReference.xlsx')

#However, after running the ExecuteDataLoad tool, datetime is modified, so now:
print(datetime)
#returns <class 'datetime.datetime'>

#which means the code
print(datetime.datetime.now())
#that previously worked, suddenly now
#returns AttributeError: type object 'datetime.datetime' has no attribute 'datetime'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The problem is, first off, this is confusing as can be, and makes reading/following the code rather confusing that there's an undocumented shift in what is defined as datetime.  However, second, and more worrisome, is the fact that I have to assume import arcpy includes an import of datetime for a reason.  What I'm getting at is, if arcpy assumes datetime will be the datetime module and calls like datetime.datetime.now() (and others) will work in that specific syntax.  Then isn't there a potential that a tool, only callable within arcpy, modifying that core assumption, could cause issues with other parts of arcpy...?

Am I over-thinking this or missing something?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
AlixVezina
Esri Regular Contributor

Hi John Streeb‌,

Thank you for reporting this, our developer will fix this issue for the upcoming release of the tools.

Regards

View solution in original post

0 Kudos
2 Replies
AlixVezina
Esri Regular Contributor

Hi John Streeb‌,

Thank you for reporting this, our developer will fix this issue for the upcoming release of the tools.

Regards

0 Kudos
PeterWiringa
New Contributor III

I just ran into this and this appears to go back years, at least as far as import arcpy overriding datetime.

See https://community.esri.com/t5/python-questions/import-arcpy-tramples-datetime-import/m-p/288325

Alix, are you able to tell us what release this is scheduled to be fixed in? I see it's still present in ArcPy shipping with Pro 2.7.x.

 

0 Kudos