AttributeError module ‘DateTime’ has no attribute ‘strptime’ (Fixed)

a page of journal with dates written on it. At the bottom it reads: Today is the perfect day to be happy!

AttributeError module ‘DateTime’ has no attribute ‘strptime’

The 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.

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.

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 script (or module). This .py file is known as a Python module.

The datetime module contains classes for manipulating dates and times. The datetime module 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 "AttributeError module 'DateTime' has no attribute 'strptime'"

You have two options to fix this attribute error:

  1. Call the method on the class datetime
  2. import the class datetime directly

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.

Author photo

Hey 👋 I'm a software engineer, an author, and an open-source contributor. I enjoy helping people (including myself) decode the complex side of technology. I share my findings on Twitter: @lavary_

If you read this far, you can tweet to the author to show them you care. Tweet a Thanks

In this article:

Disclaimer: This post might contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.