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.
Unfortunately, selectively importing classes or functions won't address the issue for Damon
Python 3.6.10 |Anaconda, Inc.| (default, May 20 2020, 01:49:13) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2020, 9, 18, 8, 13, 54, 315215)
>>>
>>> from arcpy.management import CreateTable
>>> datetime.now()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'datetime' has no attribute 'now'
>>> because dependencies within arcpy submodules end up loading nearly all (if not all) the modules anyways:
Python 3.6.10 |Anaconda, Inc.| (default, May 20 2020, 01:49:13) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(*(mod for mod in sys.modules.keys() if mod.startswith('arc')), sep='\n')
>>> from arcpy.management import CreateTable
>>> print(*(mod for mod in sys.modules.keys() if mod.startswith('arc')), sep='\n')
arcpy
arcpy.geoprocessing
arcpy.geoprocessing._base
arcgisscripting
arcgisscripting._arcgisscripting
arcpy.toolbox
arcpy.ddd
arcpy.arcobjects
arcpy.arcobjects._base
arcpy.arcobjects.arcobjectconversion
arcpy.arcobjects.arcobjects
arcpy.arcobjects.mixins
arcpy.arcobjects.geometries
arcpy._mp
arcpy.pdfdocument
arcpy.utils
arcpy._symbology
arcpy._renderer
arcpy._colorizer
arcpy.metadata
arcpy.reviewer
arcpy.agolservices
arcpy.analysis
arcpy.aviation
arcpy.ba
arcpy.ca
arcpy.cartography
arcpy.conversion
arcpy.defense
arcpy.edit
arcpy.ga
arcpy._ga
arcpy._base
arcpy.gapro
arcpy.geoanalytics
arcpy.geocoding
arcpy._geocoding
arcpy.indoors
arcpy.intelligence
arcpy.locref
arcpy.lr
arcpy.management
arcpy.maritime
arcpy.md
arcpy.na
arcpy._na
arcpy._na._solvers
arcpy._na._sas
arcpy._na._rs
arcpy._na._odcms
arcpy._na._vrps
arcpy._na._cfs
arcpy._na._las
arcpy._na._nds
arcpy.nd
arcpy.parcel
arcpy.ra
arcpy.server
arcpy.sfa
arcpy.stats
arcpy.stpm
arcpy.td
arcpy.tn
arcpy.topographic
arcpy.un
arcpy.wmx
arcpy._wmx
arcpy_wmx
arcpy_wmx._arcpy_wmx
arcpy.interop
arcpy.da
arcpy.mp
arcpy.sharing
arcpy._chart
arcpy.nax
arcpy.sa
arcpy.sa.Raster
arcpy.sa.Utils
arcpy.sa.Functions
arcpy.sa.ParameterClasses
arcpy.sa.CompoundParameter
arcpy.ia
arcpy.ia.Functions
arcpy.ia._ia
arcpy.ia.util
arcpy.ia.RasterToXarray
arcpy.ia.raster_functions
arcpy.ia.RasterCollection
arcpy.ia.PixelBlockCollection
arcpy.time
arcpy._utbx
arcpy.cmanagers
arcpy.cmanagers.EnvManager
>>>
Welll, putting the garden away for the winter, of course leads to a solution ![]()
from datetime import datetime as thyme
dir(thyme)
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
thyme.now()
datetime.datetime(2020, 9, 18, 14, 53, 50, 566500)
from arcpy.management import CreateTable
thyme.now()
datetime.datetime(2020, 9, 18, 14, 54, 22, 867106)Nothing spices up your coding as a workaround
Given that the module and class names are the same, i.e., datetime, I think that is one reason you see a lot of people import the class and rename it, like what you suggest. I know 'dt' is very common alias for datetime class.
So i tried the alias route (and appreciate the spice puns), but then something else didn't work later in the code. So I ultimately opted to simply import the datetime stuff after arcpy.
And pardon the ignorance, so does this mean that arcpy has a class somewhere called datetime? I understand that arcpy is clobbering the namespace, but I'm unclear as to why. And wouldn't importing datetime after arcpy mean that datetime is potentially clobbering something for arcpy?
Is there any update to this thread? Official documentation, best practices, future pathways?
Based on others still 'crashing' into this namespace issue, I'd wager, no?
https://stackoverflow.com/questions/12906402/type-object-datetime-datetime-has-no-attribute-datetime...
See BUG-000125894 for ArcGIS Pro, Esri has no plans to address.