How to Solve Python AttributeError: module ‘datetime’ has no attribute ‘now’

by | Programming, Python, Tips

This error occurs when you import the datetime module and try to call the now() method on the imported module. You can solve this error by importing the datetime class using from datetime import datetime or access the class method using

datetime.datetime.now()

This tutorial will go through the error and how to solve it with code examples.


AttributeError: module ‘datetime’ has no attribute ‘now’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. datetime is a built-in Python module that supplies classes for manipulating dates and times. One of the classes in datetime is called datetime. It can be unclear when both the module and one of the classes share the same name. If you use the import syntax:

import datetime

You are importing the datetime module, not the datetime class. We can verify that we are importing the module using the type() function:

import datetime

print(type(datetime))
<class 'module'>

We can check what names are under datetime using dir() as follows:

import datetime

attributes = dir(datetime)

print('now' in attributes)

In the above code, we assign the list of attributes returned by dir() to the variable name attributes. We then check for the now() attribute in the list using the in operator. When we run this code, we see it returns False.

False

However, if we import the datetime class and call dir(), we will see now as an attribute of the class. We can check for now in the list of attributes as follows:

from datetime import datetime

attributes = dir(datetime)

print('now' in attributes)
True

Example

Consider the following example, where we want to get the current local date and time.

import datetime

date = datetime.now()

Let’s run the code to see the result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 date = datetime.now()

AttributeError: module 'datetime' has no attribute 'now'

The error occurs because we imported the module datetime and tried to call the now() method, but now() is an attribute of the datetime class, not the module.

Solution #1: Use the from keyword

We can solve this error by importing the datetime class using the from keyword. Let’s look at the revised code:

from datetime import datetime

date = datetime.now()

print(date)

Let’s run the code to see the result:

2022-05-18 22:59:50.053400

We successfully retrieved the current date and time.

Solution #2: Use datetime.datetime

We can also solve this error by importing the module and then accessing the class attribute using datetime.datetime, then we can call the now() method. Let’s look at the revised code:

import datetime

date = datetime.datetime.now()

print(date)

Let’s run the code to see the result:

2022-05-18 23:43:37.372667

We successfully retrieved the current date and time.

Summary

Congratulations on reading to the end of this tutorial! Remember that from datetime import datetime imports the datetime class and import datetime imports the datetime module.

For further reading on AttributeErrors involving datetime, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨