Attributeerror: module datetime has no attribute strptime [SOLVED]

In this article, we will cover various solutions to Attributeerror: module datetime has no attribute strptime.

Apart from that, we will have example codes and output to have a better understanding of this error.

What is the module datetime has no attribute strptime?

The error AttributeError module ‘datetime’ has no attribute ‘strptime’ occurs when we try to call the strptime method directly on the datetime module.

To fix this error, use the following import import datetime and call the strptime method as datetime.datetime.strptime(…).

Here is an example of how the error occurs.

import datetime

dt = datetime.strptime("21/11/22 09:30", "%d/%m/%y %H:%M")

print(dt)

Here is the output error:

Attributeerror module datetime has no attribute strptime error
Attributeerror module datetime has no attribute strptime error

The issue is that we are trying to call the strptime method directly on the datetime module.

How to fix Attributeerror: module datetime has no attribute strptime

Here are the following solutions we can try to solve Attributeerror: module datetime has no attribute strptime error.

Call the strptime method on the datetime class instead

To fix the error just call the time strptime method instead of datetime class.

import datetime

dt = datetime.datetime.strptime(
  "21/03/23 09:30",
  "%d/%m/%y %H:%M"
)
print(dt) #2023-03-21 09:30:00

Here is the result:

2023-03-21 09:30:00

Take note, do not name your local module datetime.py, hence it would shadow hte official datetime module.

Import the datetime class directly

Another way to get rid of module datetime has no attribute strptime is importing datetime class directly.

All you have to do is change the import statement to datetime class from the datetime module.

In order not to have datetime .datetime which looks quite confusing.

from datetime import datetime

dt = datetime.strptime("21/03/23 09:30", "%d/%m/%y %H:%M")

print(dt)  #2023-03-21 09:30:00

The code above explains that datetime class is imported from datetime module and then called the strptime method on the class.

It is a bit confusing because we have datetime class at the same time datetime module.

Hence, the following we presenting detailed.

from datetime import datetime

#<class 'datetime.datetime'>
print(datetime)

The code above shows it import datetime class. Meanwhile following import statement imports the entire datetime module.

import datetime

print(datetime)

# <class 'datetime.datetime'>
print(datetime.datetime)

Here is the output, where the error is removed:

2023-03-21 09:30:00

Use Alias

Another way is to use aliases to make the code easier to read in the import statements.

So here is how it looks to have an alias in your code:

#alias datetime class to dt
from datetime import datetime as dt

result = dt.strptime("21/03/23 09:30", "%d/%m/%y %H:%M")

print(result)  #2023-03-21 09:30:00

In our example code above we use alias dt for the datetime class.

Therefore every time we will call the method strptime we use ‘dt.strptime() instead of datetime.strptime().

Here is the output:

2023-03-21 09:30:00

Conclusion

In conclusion, the error Attributeerror: module datetime has no attribute strptime can be easily solved by importing import datetime and call the strptime method as datetime.datetime.strptime(…).

By following the guide above, there’s no doubt that you’ll be able to resolve this error quickly.

We hope you’ve learned a lot from this.

If you are finding solutions to some errors you might encounter we also have Typeerror object of type float32 is not json serializable.

Thank you for reading!

Leave a Comment