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(…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Reza Lavarian

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'

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')

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)

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

❤️ You might like:


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Reza Lavarian


Print Share Comment Cite Upload Translate Updates
APA

Reza Lavarian | Sciencx (2023-02-04T16:04:56+00:00) About “AttributeError module ‘DateTime’ has no attribute ‘strptime’” in Python. Retrieved from https://www.scien.cx/2023/02/04/about-attributeerror-module-datetime-has-no-attribute-strptime-in-python/

MLA
" » About “AttributeError module ‘DateTime’ has no attribute ‘strptime’” in Python." Reza Lavarian | Sciencx - Saturday February 4, 2023, https://www.scien.cx/2023/02/04/about-attributeerror-module-datetime-has-no-attribute-strptime-in-python/
HARVARD
Reza Lavarian | Sciencx Saturday February 4, 2023 » About “AttributeError module ‘DateTime’ has no attribute ‘strptime’” in Python., viewed ,<https://www.scien.cx/2023/02/04/about-attributeerror-module-datetime-has-no-attribute-strptime-in-python/>
VANCOUVER
Reza Lavarian | Sciencx - » About “AttributeError module ‘DateTime’ has no attribute ‘strptime’” in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/04/about-attributeerror-module-datetime-has-no-attribute-strptime-in-python/
CHICAGO
" » About “AttributeError module ‘DateTime’ has no attribute ‘strptime’” in Python." Reza Lavarian | Sciencx - Accessed . https://www.scien.cx/2023/02/04/about-attributeerror-module-datetime-has-no-attribute-strptime-in-python/
IEEE
" » About “AttributeError module ‘DateTime’ has no attribute ‘strptime’” in Python." Reza Lavarian | Sciencx [Online]. Available: https://www.scien.cx/2023/02/04/about-attributeerror-module-datetime-has-no-attribute-strptime-in-python/. [Accessed: ]
rf:citation
» About “AttributeError module ‘DateTime’ has no attribute ‘strptime’” in Python | Reza Lavarian | Sciencx | https://www.scien.cx/2023/02/04/about-attributeerror-module-datetime-has-no-attribute-strptime-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.