DEV Community

Cover image for About "AttributeError module ‘DateTime’ has no attribute ‘strptime’" in Python
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

About "AttributeError module ‘DateTime’ has no attribute ‘strptime’" in Python

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

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'
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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:

  • Call the method on the class datetime
  • 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.datetime class - This way, you won't have to change your import statement.

import datetime

date_string = '25 December, 2022'
datetime_object = datetime.datetime.strptime(date_string, '%d %B, %Y')

print(datetime_object)
Enter fullscreen mode Exit fullscreen mode

The expression datetime.datetime refers to the datetime class in the datetime module.

Here's the output:

2022-12-25 00:00:00
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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.


❤️ You might like:

Top comments (1)

Collapse
 
Sloan, the sloth mascot
Comment deleted