How To Resolve: AttributeError Module ‘datetime’ Has No Attribute ‘now’ In Python?

AttributeError module ‘datetime’ has no attribute ‘now’

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 the now() 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:

Leave a Reply

Your email address will not be published. Required fields are marked *