Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have gotten the following error:

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

On the following line:

date = datetime.datetime(int(year), int(month), 1) 

Does anybody know the reason for the error?

I imported datetime with from datetime import datetime if that helps

Thanks

like image 205
Chris Frank Avatar asked Oct 16 '12 01:10

Chris Frank


People also ask

Does Utcnow have no attribute?

The error "AttributeError module 'datetime' has no attribute 'utcnow'" occurs when we try to call the utcnow method directly on the datetime module. To solve the error, use the following import import datetime and call the utcnow method as datetime. datetime. utcnow() .

How do I convert datetime to date in python?

The DateTime value is then converted to a date value using the dt. date() function.


1 Answers

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.

like image 145
John Lyon Avatar answered Oct 12 '22 04:10

John Lyon