import arcpy tramples datetime import

2991
23
07-14-2016 11:14 AM
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
MVP Esteemed Contributor

People had better scrounge around in their __init__.py files.

Perhaps the import should check for the existence in the namespace at least or use names that are unlikely to occur.

c:\\arcpro\\resources\\arcpy\\arcpy\\__init__.py....

import arcpy.time as time  # ????

#how about, _time or thyme or time_ or about‍‍‍_ or tImE
Reply
0 Kudos
Occasional Contributor II
import time, math, sys
from datetime import datetime
map(id, [time, math, sys, datetime])
[48738160, 57747920, 47497264, 1699354320]
import arcpy
map(id, [time, math, sys, datetime])
[48738160, 57747920, 47497264, 57866672]‍‍‍‍‍‍‍

The only ID that changes is datetime, so I'm not sure if that's actually the case. But we can look further into it as well. 

Reply
0 Kudos
New Contributor II

Clinton Dow‌ Was this bug resolved and at what ArcGIS version? It would be great to know. I just ran into it on ArcGIS 10.4.1 and lost a few hours trying to figure out what was happening.

Reply
0 Kudos
MVP Frequent Contributor
# ---- python 3.10, arcpy 2.6 beta 2
import time, math, sys
from datetime import datetime
list(map(id, [time, math, sys, datetime]))
[1908941284840, 1908942659400, 1908930535160, 1381901040]

import arcpy
list(map(id, [time, math, sys, datetime]))
[1908941284840, 1908942659400, 1908930535160, 1381901040]

# ---- looks good

... sort of retired...
Reply
0 Kudos
New Contributor II

Thanks for the info! Would you know at what version of ArcGIS this bug was fixed? In any case, I'll endeavor to upgrade to the latest version.

Reply
0 Kudos
MVP Frequent Contributor

Not sure, but I think shortly after it was discovered.  I don't remember it being an issue for a few years


... sort of retired...
Reply
0 Kudos
Occasional Contributor

I just experienced this issue on ArcGIS Pro 2.5.2 (i.e. Python v 3).  I only discovered it after getting the "now()" error and then manually moving my "from datetime import datetime" statement until i figure out it was the arcpy import that was clobbering it.  So maybe not fixed, unless i'm doing something wrong?

Reply
0 Kudos
MVP Frequent Contributor

hmmmm. I always import arcpy last

from datetime import datetime

n = datetime.now()

n
datetime.datetime(2020, 9, 17, 22, 7, 43, 827042)

import arcpy

n1 = datetime.now()

n1
datetime.datetime(2020, 9, 17, 22, 8, 0, 770167)

... sort of retired...
Reply
0 Kudos
Occasional Contributor

So slight wrinkle.  It doesn't have the issue when you're using the interactive python window in Pro, but it DOES have the issue when using in a standalone script.  I'm using the propy.bat to run the script from a windows scheduled task.

Reply
0 Kudos
MVP Frequent Contributor

As a suggestion, I rarely import arcpy If I need something from the arcpy stable, I usually narrow down the import to the area that I need.  The folder structure can be found in your install base folder

C:\arc_pro\Resources\ArcPy\arcpy  where arc_pro is my install folder.

Some things like the arcgis module and the arcgisscripting module can be found in site-packages

C:\arc_pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgisscripting

So a blanket import of arcpy isn't recommended unless you need to access a wide variety of things from it.


... sort of retired...
Reply
0 Kudos