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

by | Programming, Python, Tips

This error occurs when you import the datetime class from the datetime module using

from datetime import datetime

and then try to create a datetime object using the class constructor datetime.datetime().

You can solve this error by removing the extra datetime when creating a datetime object or using:

import datetime

instead of:

from datetime import datetime

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


AttributeError: ‘datetime.datetime’ has no attribute ‘datetime’

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:

from datetime import datetime

You are importing the datetime class, not the datetime module. We can find the attributes of an object of the datetime class using the built-in dir() function.

from datetime import datetime

# dir of object of datetime class
obj = datetime(1999, 12, 31)

attributes = dir(obj)

print('datetime' in attributes)

In the above code, we created an object of the datetime class the assigned its list of attributes returned by dir() to the variable name attributes. We then check for the datetime attribute in the list using the in operator. When we run this code we see it returns False.

False

We can see that datetime is not an attribute of an object of the datetime class.

However, if we import the datetime module and call the dir function as we have done above, we will see that datetime is an attribute of the datetime module

import datetime
# dir of datetime module
attributes = dir(datetime)

print('datetime' in attributes)
True

The above list shows that datetime is a class within the datetime module. Next, we will use an example to demonstrate and solve the error.

Example

Let’s look at an example of creating a datetime object. The datetime class requires three parameters to create a date: year, month, and day.

from datetime import datetime

date = datetime.datetime(2022, 6, 17)

print(date)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [4], in <cell line: 3>()
      1 from datetime import datetime
----> 3 date = datetime.datetime(2022, 6, 17)
      5 print(date)

AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

The error occurs because we imported the datetime class. When we try to create a date object using datetime.datetime we are trying to call datetime.datetime.datetime, which does not exist.

Solution #1: Remove extra datetime

We can solve this error by removing the extra datetime, as we have imported the datetime class, creating an object of the class only requires the datetime() class constructor.

from datetime import datetime

date = datetime(2022, 6, 17)

print(date)

Let’s run the code to see the result:

2022-06-17 00:00:00

We successfully created a date object.

Solution #2: Use import datetime

The second way to solve this error is to import the datetime module and then access the class constructor through datetime.datetime(). The first datetime is the module name, and the second is the class constructor. Let’s look at that the revised code:

import datetime

date = datetime.datetime(2022, 6, 17)

print(date)

Let’s run the code to see the result:

2022-06-17 00:00:00

We successfully created a date object.

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 article:

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!