type object 'datetime.datetime' has no attribute 'datetime'

前端 未结 8 1578
情深已故
情深已故 2020-11-28 03:37

I have gotten the following error:

type object \'datetime.datetime\' has no attribute \'datetime\'

On the following line:

<
相关标签:
8条回答
  • 2020-11-28 04:35

    I run into the same error maybe you have already imported the module by using only import datetime so change form datetime import datetime to only import datetime. It worked for me after I changed it back.

    0 讨论(0)
  • 2020-11-28 04:39

    Datetime is a module that allows for handling of dates, times and datetimes (all of which are datatypes). This means that datetime is both a top-level module as well as being a type within that module. This is confusing.

    Your error is probably based on the confusing naming of the module, and what either you or a module you're using has already imported.

    >>> import datetime
    >>> datetime
    <module 'datetime' from '/usr/lib/python2.6/lib-dynload/datetime.so'>
    >>> datetime.datetime(2001,5,1)
    datetime.datetime(2001, 5, 1, 0, 0)
    

    But, if you import datetime.datetime:

    >>> from datetime import datetime
    >>> datetime
    <type 'datetime.datetime'>
    >>> datetime.datetime(2001,5,1) # You shouldn't expect this to work 
                                    # as you imported the type, not the module
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
    >>> datetime(2001,5,1)
    datetime.datetime(2001, 5, 1, 0, 0)
    

    I suspect you or one of the modules you're using has imported like this: from datetime import datetime.

    0 讨论(0)
提交回复
热议问题