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

PythonDatetimeNamespaces

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

For python 3.3

from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)

Solution 3 - Python

You should use

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

Or change

from datetime import datetime

to

import datetime

Solution 4 - Python

You should really import the module into its own alias.

import datetime as dt
my_datetime = dt.datetime(year, month, day)

The above has the following benefits over the other solutions:

  • Calling the variable my_datetime instead of date reduces confusion since there is already a date in the datetime module (datetime.date).
  • The module and the class (both called datetime) do not shadow each other.

Solution 5 - Python

If you have used:

from datetime import datetime

Then simply write the code as:

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

But if you have used:

import datetime

then only you can write:

date = datetime.datetime(int(2005), int(5), 1)

Solution 6 - Python

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

Solution 7 - Python

import time
import datetime
from datetime import date,timedelta

You must have imported datetime from datetime.

Solution 8 - Python

I found this to be a lot easier

from dateutil import relativedelta
relativedelta.relativedelta(end_time,start_time).seconds

Solution 9 - Python

from datetime import datetime
import time
from calendar import timegm
d = datetime.utcnow()
d = d.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
utc_time = time.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
epoch_time = timegm(utc_time)

Solution 10 - Python

Avoid to write:

from datetime import datetime
datetime.datetime.function()

Solution No. 1:

import datetime
datetime.datetime.function()

Solution No. 2:

from datetime import datetime
datetime.function()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris FrankView Question on Stackoverflow
Solution 1 - PythonJohn LyonView Answer on Stackoverflow
Solution 2 - PythonRouRView Answer on Stackoverflow
Solution 3 - PythonwaitingkuoView Answer on Stackoverflow
Solution 4 - PythonRobinoView Answer on Stackoverflow
Solution 5 - PythonM. PaulView Answer on Stackoverflow
Solution 6 - PythonAudrey MengueView Answer on Stackoverflow
Solution 7 - PythonATHARVA HIWASEView Answer on Stackoverflow
Solution 8 - PythonKamaldeep SinghView Answer on Stackoverflow
Solution 9 - PythonJay AgrawalView Answer on Stackoverflow
Solution 10 - PythonNastaran SfView Answer on Stackoverflow