[Solved] AttributeError: ‘module’ object has no attribute ‘strptime’

The AttributeError: ‘module’ object has no attribute ‘strptime’ occurs if you have imported the datetime module and directly if we are using the datetime.strptime() method on the datetime module. 

The datetime is a module, and it does not have the strptime() method; instead, we need to use the datetime class name, which has the method correct method and the syntax for the same is datetime.datetime.strptime() 

In this tutorial, we will look into what exactly is AttributeError: ‘module’ object has no attribute ‘strptime’ and how to resolve the error with examples.

What is AttributeError: ‘module’ object has no attribute ‘strptime’

First, let us see how to reproduce this issue and why developers face this particular issue with a simple example.

# import datetime module
import datetime

start_date = "2022-05-06"

# convert into datetime object and print
print(datetime.strptime(start_date, "%Y-%m-%d"))

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\Code.py", line 7, in <module>
    print(datetime.strptime(start_date, "%Y-%m-%d"))
AttributeError: module 'datetime' has no attribute 'strptime'

In the above example, we are importing the datetime module and trying to convert the string datetime to a datetime object using the datetime.strptime() method.

When we run the code, we get an AttributeError: module ‘datetime’ has no attribute ‘strptime’

The issue occurs because the datetime module does not have a strptime() method, and hence it is throwing an error.

The datetime module has a class name called datetime which in turn has the method strptime()

Since the module name and class name are also the same, it leads to a lot of confusion for the new developers, and they feel it’s ambiguous to use datetime multiple times.

How to resolve AttributeError: ‘module’ object has no attribute ‘strptime’

We can resolve the ‘module’ object has no attribute ‘strptime’ by using the strptime() method, which is present inside the datetime class.

There are two ways to access the strptime() method correctly.

Solution 1: Import the datetime module directly and access the method through its class name

If you are importing the datetime module directly, then the best way to resolve the error is to use datetime.datetime.strptime() method.

Syntax

datetime.datetime.strptime()

Here the first datetime is a module and the second datetime is a class which has a method strptime()

Example – 

# import datetime module
import datetime

start_date = "2022-05-06"

# convert into datetime object and print
print(datetime.datetime.strptime(start_date, "%Y-%m-%d"))

Output

2022-05-06 00:00:00

Approach 2 – Import the datetime class from the datetime module

Another way to resolve the issue is to import the datetime class directly using the syntax from datetime import datetime

Syntax

from datetime import datetime

While using the from syntax, we import the datetime class directly and using the class name; we can access all of its methods. We do not have to prefix/use the module name here.

Example –

# import datetime class from datetime module
from datetime import datetime

start_date = "2022-05-06"

# convert into datetime object and print
print(datetime.strptime(start_date, "%Y-%m-%d"))

Output

2022-05-06 00:00:00

Conclusion

The datetime module does not have the strptime() method; hence if we try to use datetime.strptime() directly we get AttributeError: ‘module’ object has no attribute ‘strptime’

We can resolve the issue using the datetime class name instead of the datetime module. An alternate way is to import the datetime class using the from keyword directly.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Type()

Python type()

Table of Contents Hide type() Syntaxtype() Parameterstype() Return ValueDifference between type() function and isinstance() functionExample of isinstance() method in PythonExample 1: Finding the type of a Python objectExample 2: type()…
View Post