AttributeError: module 'datetime' has no attribute 'now' (tested fix)
AttributeError: module 'datetime' has no attribute 'now' and its mirror image type object 'datetime.datetime' has no attribute 'datetime' both come from the datetime module having a class with the same name. We ran nine common calls under both import styles to show exactly which spelling goes with which import, plus the two less obvious causes.
1. Which spelling works with which import
The datetime module contains a datetime class, and both are usually called datetime in your code. Every one of these errors comes from mixing them up. We ran each common line with each import style:
def run(code):
try:
exec(code, {})
print(f"{code!r:60} OK")
except Exception as e:
print(f"{code!r:60} {type(e).__name__}: {e}")
run("import datetime; datetime.now()")
run("import datetime; datetime.strptime('2026-10-22', '%Y-%m-%d')")
run("import datetime; datetime.datetime.now()")
run("from datetime import datetime; datetime.datetime.now()")
run("from datetime import datetime; datetime.now()")
run("from datetime import datetime; datetime.timedelta(days=1)")
run("from datetime import datetime, timedelta; timedelta(days=1)")
run("import datetime; datetime.date.today()")
run("from datetime import datetime; datetime.date.today()")
Output:
'import datetime; datetime.now()' AttributeError: module 'datetime' has no attribute 'now'
"import datetime; datetime.strptime('2026-10-22', '%Y-%m-%d')" AttributeError: module 'datetime' has no attribute 'strptime'
'import datetime; datetime.datetime.now()' OK
'from datetime import datetime; datetime.datetime.now()' AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
'from datetime import datetime; datetime.now()' OK
'from datetime import datetime; datetime.timedelta(days=1)' AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
'from datetime import datetime, timedelta; timedelta(days=1)' OK
'import datetime; datetime.date.today()' OK
'from datetime import datetime; datetime.date.today()' AttributeError: 'method_descriptor' object has no attribute 'today'
The rule:
- After
import datetime, the name is the module: writedatetime.datetime.now(),datetime.datetime.strptime(...),datetime.date.today(),datetime.timedelta(...). - After
from datetime import datetime, the name is the class: writedatetime.now(),datetime.strptime(...), and importdate/timedeltaseparately.
Read the error to tell which one you have: module 'datetime' has no attribute ... means the name is the module, while type object 'datetime.datetime' has no attribute ... means it is the class. The oddest one is 'method_descriptor' object has no attribute 'today': on the class, datetime.date is the method that returns the date part of a datetime, not the date class.
2. Both imports in one file: the second one wins
A file that has both import datetime and from datetime import datetime works or fails depending on which line comes last:
import datetime # line 1: the module
from datetime import datetime # line 2: rebinds the name to the class
print(type(datetime), datetime)
try:
datetime.timedelta(days=1)
except AttributeError as e:
print("AttributeError:", e)
Output:
<class 'type'> <class 'datetime.datetime'>
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
The second import replaced the module with the class, so every datetime.timedelta and datetime.date in the file breaks, often far from the imports. This also happens in notebooks, where a cell run later changes the name for every cell after it. Pick one style per file.
3. Your own file named datetime.py
If module 'datetime' has no attribute 'datetime' appears even with correct code, check for a file called datetime.py next to your script:
# shadow/datetime.py
print("this is my own datetime.py")
# shadow/main.py (run from the shadow/ folder)
import datetime
print(datetime.__file__.rsplit("/", 2)[-2:])
try:
datetime.datetime.now()
except AttributeError as e:
print("AttributeError:", e)
Output:
this is my own datetime.py
['shadow', 'datetime.py']
AttributeError: module 'datetime' has no attribute 'datetime' (consider renaming '/tmp/dt/shadow/datetime.py' since it has the same name as the standard library module named 'datetime' and prevents importing that standard library module)
Python imported the local file instead of the standard library. Python 3.13 adds the "consider renaming" hint shown above, and older versions only print the bare AttributeError. Rename the file. More on this pattern in our module has no attribute guide.
4. The style that avoids all of it
import datetime as dt # one module alias, no name clashes
now = dt.datetime.now()
print(now.date(), "tomorrow:", (now + dt.timedelta(days=1)).date() > now.date())
print(dt.date.today() == now.date(), dt.datetime.now(dt.timezone.utc).tzinfo)
Output:
2026-10-22 tomorrow: True
True UTC
import datetime as dt keeps one short name for the module, and nothing in your code can clash with the class. Comparing naive and aware datetimes is a separate error: see can't compare offset-naive and offset-aware datetimes, and for parsing errors strptime time data does not match format.
Frequently asked questions
Why does Python say module 'datetime' has no attribute 'now'?
You wrote import datetime, so datetime is the module. now() lives on the class: use datetime.datetime.now(), or change the import to from datetime import datetime.
What does type object 'datetime.datetime' has no attribute 'datetime' mean?
You wrote from datetime import datetime, so datetime is already the class. Use datetime.now() instead of datetime.datetime.now().
Why does datetime.date.today() give 'method_descriptor' object has no attribute 'today'?
After from datetime import datetime, datetime.date is the datetime.date() method, not the date class. Import it with from datetime import date and call date.today().
How do I avoid datetime import confusion?
Use import datetime as dt and write dt.datetime.now(), dt.date.today(), dt.timedelta(days=1). Don't use both import styles in one file, and don't name your own file datetime.py.
Written with AI assistance. Measured on 2026-10-22 on Debian with Python 3.13.5: nine common datetime calls under both import styles, both imports in one file, and a local datetime.py shadowing the standard library. Every block was run verbatim and its output pasted unedited. PlainHeaders is our extension; Python is free.