AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ in Python (2 Examples)
In this tutorial, you’ll learn how to deal with the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ in the Python programming language.
The page is structured as follows:
Let’s get started…
Example 1: Reproduce the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’
In Example 1, I’ll explain how to replicate the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ in the Python programming language.
Before we can start, we have to load the datetime module as you can see here:
from datetime import datetime # Import datetime
Then we try to create a datetime object with the following code:
x = datetime.datetime(2022, 8, 6) # Creating datetime object does not work # AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
As you can see, the Python console returns AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ after executing the previous Python syntax.
Let’s solve this problem!
Example 2: Debug the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’
In Example 2, I’ll show how to fix the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’.
To resolve the problem, we just have to “import datetime” instead of “from datetime import datetime” as we did it above.
import datetime # Import datetime
Similar to the first example, we can now use the code below to create our datetime object:
x = datetime.datetime(2022, 8, 6) # Creating datetime object works properly print(x) # Print properly created datetime object # 2022-08-06 00:00:00
Looks good. This time we receive a proper output and no error messages are returned anymore.
Similar Error Messages
When working with the datetime module in Python, there can also be error messages related to AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’
Below, you can find a list of similar errors:
- AttributeError: type object ‘datetime.datetime’ has no attribute ‘timedelta’
- AttributeError: ‘datetime.datetime’ object has no attribute ‘timestamp’
- AttributeError: module ‘datetime’ has no attribute ‘strftime’
- AttributeError: type object ‘datetime.datetime’ has no attribute ‘fromisoformat’
- AttributeError: module ‘datetime’ has no attribute ‘strptime’
- AttributeError: ‘datetime.datetime’ object has no attribute ‘split’
- AttributeError: ‘datetime.datetime’ object has no attribute ‘read’
Video, Further Resources & Summary
Have a look at the following video on my YouTube channel. I explain the Python code of this article in the video:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Besides the video, you could read the other posts on my website. Some articles about topics such as character strings, dates, data objects, and data conversion can be found below:
- Python AttributeError datetime module has no attribute strptime
- Convert String to datetime Object in Python
- Convert datetime Object to Date Only String in Python
- Convert Object Data Type to String in pandas DataFrame Column in Python
- Convert datetime Object to Seconds, Minutes & Hours in Python
- All Python Programming Tutorials
In this tutorial, I have demonstrated how to handle the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ in the Python programming language. Don’t hesitate to kindly let me know in the comments below, in case you have any additional questions. In addition, please subscribe to my email newsletter to receive updates on new articles.
This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.