The error “AttributeError module ‘datetime’ has no attribute ‘now’” happens when you call the now() attribute. To fix the error, let follow the article to better understand.
AttributeError module ‘datetime’ has no attribute ‘now’
In Python, a datetime module is responsible for working and dealing with issues related to date and time. In the datetime module, there are the following classes: datetime.date, datetime.time, datetime.datetime and datetime.delta. The now()
attribute is in class datetime.datetime. The error happens when you call the now()
attribute without importing the datetime class but only importing the datetime module.
Example:
import datetime date = datetime.now()
Output:
Traceback (most recent call last):
File "code.py", line 2, in <module>
date = datetime.now()
AttributeError: module 'datetime' has no attribute 'now'
How to solve this error?
Import datetime class
As I mentioned, the error “AttributeError module ‘datetime’ has no attribute ‘now” happens when you don’t import datetime class, so just importing datetime class and then using the from
keyword error will be solved.
Example:
- Import datetime class from datetime module.
- Call the
now()
property to print the date and time. - Outputs the current date and time.
from datetime import datetime # The now() attribute to print the date and time dateTimeNow = datetime.now() print(dateTimeNow)
Output:
2022-10-07 11:10:00.157460
Use datetime.datetime
In short, the first method is to use datetime.datetime (I also access the module and import the datetime class), then call the now()
attribute, and the error will be resolved.
Example:
- Import datetime module.
- Use
datetime.datetime
and call thenow()
attribute. - Print out the date and time.
import datetime # Use the now() attribute to print the date and time dateTimeNow = datetime.datetime.now() print(dateTimeNow)
Output:
2022-10-07 11:18:37.885667
Use the assigned name
To avoid overlapping the class name with the module name, you can handle it by assigning it a different name.
Example:
- Import datetime class from datetime module.
- Assign a different name than the module name and class name. In this example, I named it
lsi
. - Outputs the current date and time.
from datetime import datetime as lsi print(lsi.now())
Output:
2022-10-07 11:28:47.856817
Note:
Use the dir()
function to check the module’s attributes.
Example:
import datetime namesAttr = dir(datetime) print(namesAttr)
Output:

You can see now()
property is not present in the datetime module, so import datetime class to avoid the error.
Summary
If you have any questions about the “AttributeError module ‘datetime’ has no attribute ‘now'” error in Python, please leave a comment below. We will support your questions. Thanks for reading!
Maybe you are interested in similar errors:
- AttributeError module ‘time’ has no attribute ‘clock’
- AttributeError: ‘dict’ object has no attribute ‘append’
- AttributeError: ‘dict’ object has no attribute ‘has_key’ in Python
- AttributeError: ‘list’ object has no attribute ‘items’ in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java