247

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

16 Answers 16

356

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.

2
  • 6
    @Jean this is the fault of datetime shadowing its own module's name with one of its class names, not python's.
    – Robino
    Commented Feb 16, 2018 at 12:34
  • 8
    This is one of the most frustratingly silly aspects of datetime. The person who programmed it should do some refactoring.
    – Nav
    Commented Feb 23, 2022 at 15:37
183

For python 3.3

from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)
3
  • 17
    +1. I think this should be the correct answer because as far as I know, import datetime always causes problems, so I'm in favor of a solution which uses from datetime import datetime
    – jeff
    Commented Feb 24, 2015 at 18:54
  • This is the best answer. Other answers are confusing. This should have been the accepted answer. Commented Mar 28, 2021 at 14:58
  • I personally liked this one because it showed the correct usage of timedelta when using the from X import Y method. Commented Apr 22, 2021 at 2:44
31

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.
1
  • 1
    To me this is really the best solution to avoid the repeated confusion between datetime... and datetime.datetime...
    – 8forty
    Commented May 10, 2023 at 22:35
25

You should use

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

Or change

from datetime import datetime

to

import datetime
0
9

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)
5

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.

1
  • 1
    Is the form a typo? Seems like it should be from.
    – Yunnosch
    Commented Apr 13, 2021 at 7:38
3
import time
import datetime
from datetime import date,timedelta

You must have imported datetime from datetime.

3

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()
2

I found this to be a lot easier

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

You can simply run the below command.

import datetime

datetime.fromtimestamp(details.start_date).strftime("%Y-%m-%d %H:%M:%S")

1
  • I'm not sure that really answers the question the person wrote; a number of the other answers seem to address the key problem
    – Dragon
    Commented Apr 12, 2023 at 1:54
0
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)
1
  • 12
    Try to avoid code-only answers. You can edit your post to add some explanations and links to support your code.
    – Tomerikoo
    Commented Mar 30, 2020 at 7:36
0

delete one datetime from:

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

and you get this:

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

you already imported the first one with this:

from datetime import datetime

so its redundant.

0

I tried :

import datetime 
from datetime import timedelta

and it worked somewhat

0

You should use dt.datetime instead of datetime.datetime. It works very well like in the example below:

    import datetime as dt
    from datetime import datetime,timedelta


    string_date1="2023-11-22"
    date1=dt.datetime.strptime(string_date1, "%Y-%m-%d").date()
    print(date1)
    string_added_days="14";added_days=dt.timedelta(days=int(string_added_days))
    print(added_days)
    date_in_future=date1+added_days
    print(date_in_future)
0
from datetime import datetime, timedelta, timezone

print(datetime.today()  - datetime.timedelta(days=int(15)))

This will give you error like type object 'datetime.datetime' has no attribute 'timedelta'.

Use below will work:

from datetime import datetime, timedelta, timezone

print(datetime.today()  - timedelta(days=int(15)))
-4

The Problem Is That You Are Using The Tag

from datetime

I had The Same Problem You Need To use It Like This Instead

import datetime
1
  • Hey, welcome to SO. I am not a downvoter but suspect you are getting downvoted because this isn't completely correct, and you could make use of formatting tags to make your code references clearer.
    – ApplePie
    Commented Nov 22, 2022 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.