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

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Trim String Strip() Vs Rstrip() Vs Lstrip()

Python Trim String – rstrip(), lstrip(), strip()

Table of Contents Hide Difference between strip() vs rstrip() vs lstrip()strip()Example of strip() function  rstrip() Example of rstrip() function   lstrip() Example of lstrip() function   Python provides three methods to trim the…
View Post
Python String Index()

Python String index()

Table of Contents Hide index() Syntaxindex() Parametersindex() Return ValueDifference between index() method and find() methodExample 1: Find the index of a string in PythonExample 2: If string is not found…
View Post
How To Rename A File In Python

How to Rename a file in Python?

Table of Contents Hide Python Rename FileExample to rename a file in PythonPython Rename Multiple FilesExample to Rename Multiple Files in Python The os module in Python comes in handy…
View Post