Fix “AttributeError: module ‘DateTime’ has no attribute ‘strptime'” in Python
The Python error “AttributeError: module ‘DateTime’ has no attribute ‘strptime'” occurs when you call the strptime()
method on Python’s datetime
module – rather than the datetime
class inside it.
🎧 Debugging Jam
Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀
Here’s what it looks like:
Traceback (most recent call last):
File "", line 1, in
AttributeError: module 'datetime' has no attribute 'strptime'
If you get the above error, your code probably looks similar to this:
import datetime
date_string = '25 Dcember, 2022'
datetime_object = datetime.strptime(date_string, '%d %B, %Y')
print(datetime_object)
print(type(datetime_object))
The reason is strptime()
method isn't a function defined inside the datetime
module. It's a method implemented by a class named datetime
that happens to be inside the datetime
module.
Psssst! Do you want to learn web development in 2023?
If you're wondering what a module is in Python, here's a quick refresher:
Python lets you reuse functions, classes, etc., by defining them in a file you can import into any code (or module). This .py
file is known as a Python module.
The datetime
module contains classes for manipulating dates and times. It contains a class definition of the same name (datetime
), which implements the strptime()
method.
I know! It's like there was a city in Canada named Canada. Imagine sending a postcard there!
How to fix it?
You have two options to fix "module 'DateTime' has no attribute 'strptime'" error:
Call the method on the class datetime: One way to fix the error is to call the strptime()
method on the datetime
class - This way, you won't have to change your import statement.
The strptime()
method accepts a date string (e.g., 25 December, 2022) and returns a datetime
object; It parses the date string based on the provided format. In the following code, we parse 25 December, 2022 (🎄) into a datetime
object:
import datetime
date_string = '25 December, 2022'
datetime_object = datetime.datetime.strptime(date_string, '%d %B, %Y')
print(datetime_object)
datetime.datetime
refers to the datetime
class in the datetime
module.
Here's the output:
2022-12-25 00:00:00
""
Import the datetime
class directly: If you prefer not to touch your code, you can edit your import statement instead:
from datetime import datetime
date_string = '25 December, 2022'
datetime_object = datetime.strptime(date_string, '%d %B, %Y')
print(datetime_object)
print(type(datetime_object))
In the above code, we explicitly imported the datetime
class from the datetime
module. As a result, any datetime
instance points to the datetime
class.
By the way, always avoid naming a variable datetime
because it'll override the reference to the datetime
class. And you'll get a similar attribute error if you call the strptime()
method.
Alright, I think that does it! I hope you found this quick guide helpful.
Thanks for reading.
Never miss a guide like this!
Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.