import arcpy tramples datetime import

6354
23
07-14-2016 11:14 AM
FredSpataro
Occasional Contributor III

I've never seen anything that states arcpy should/must be imported first in order to not override other system modules.  I only skimmed this document but didn't see anything specific, although their examples tend to imply it.  However if you do a import-from on datetime BEFORE importing arcpy, then you're datetime import will be destroyed:

>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2016, 7, 14, 12, 10, 12, 433000)
>>> import arcpy
>>> datetime.now()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'now'
>>> 

Either import datetime after arcpy or use the import-from-as syntax and give datetime an alias.

Tags (1)
23 Replies
ClintonDow1
Occasional Contributor II

Which version of ArcGIS/Python are you experiencing this in?

0 Kudos
FredSpataro
Occasional Contributor III

I see it on all the ones I've tried so far:

32 bit 10.3.1

64 bit 10.3.1

32 bit 10.4

Haven't tired any others.

0 Kudos
ClintonDow1
Occasional Contributor II

Thanks. I'll test and submit a bug report.

0 Kudos
FredSpataro
Occasional Contributor III

Great, thanks.  At the very least, note it in the document about importing Arcpy.  There's always the chance of namespace/module collision but I've never seen one like this.

0 Kudos
ClintonDow1
Occasional Contributor II

As a workaround you can use:

import datetime
datetime.datetime.now()
import arcpy
datetime.datetime.now()

Or alternatively:

from datetime import datetime as dt
dt.now()
import arcpy
dt.now()

We'll still look into the actual bug as well

FredSpataro
Occasional Contributor III

Thanks, I've started making arcpy the first import and using the second option, safe aliases, a standard.

LoganPugh
Occasional Contributor III

Clinton Dow‌ has this been resolved in any released version of ArcGIS? Or is there a Nimbus # associated with it yet? I'm also running into this at 10.4.1, though it is easy enough to work around.

0 Kudos
ClintonDow1
Occasional Contributor II

Currently its logged on our internal version control system as a bug, but lower priority due to the workaround. I'll bring it up again at the next Python meeting. I figure it could be fairly problematic if arcpy is used mid-stream in a script that imports other complex packages. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

While you are talking about datetime, maybe just mention that math, sys, and time also get clobbered when importing arcpy.  I can understand arcpy importing arcgis, but I can't think of any other packages I work with that re-import base packages into the global namespace.

0 Kudos