close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: ‘datetime’ module has no attribute ‘strptime’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: ‘datetime’ module has no attribute ‘strptime’ Error Occurs?

Today I get the following error AttributeError: ‘datetime’ module has no attribute ‘strptime’ in python.

How To Solve AttributeError: ‘datetime’ module has no attribute ‘strptime’ Error ?

  1. How To Solve AttributeError: 'datetime' module has no attribute 'strptime' Error ?

    To Solve AttributeError: 'datetime' module has no attribute 'strptime' Error Use the correct call: strptime is a classmethod of the datetime.datetime class, it's not a function in the datetime module.

  2. AttributeError: 'datetime' module has no attribute 'strptime'

    To Solve AttributeError: 'datetime' module has no attribute 'strptime' Error Use the correct call: strptime is a classmethod of the datetime.datetime class, it's not a function in the datetime module.

Solution 1

f I had to guess, you did this:

import datetime

at the top of your code. This means that you have to do this:

datetime.datetime.strptime(date, "%Y-%m-%d")

to access the strptime method. Or, you could change the import statement to this:

from datetime import datetime

and access it as you are.

The people who made the datetime module also named their class datetime:

#module  class    method
datetime.datetime.strptime(date, "%Y-%m-%d")

Solution 2

Use the correct call: strptime is a classmethod of the datetime.datetime class, it’s not a function in the datetime module.

self.date = datetime.datetime.strptime(self.d, "%Y-%m-%d")

As mentioned by Jon Clements in the comments, some people do from datetime import datetime, which would bind the datetime name to the datetime class, and make your initial code work.

To identify which case you’re facing (in the future), look at your import statements

  • import datetime: that’s the module (that’s what you have right now).
  • from datetime import datetime: that’s the class.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read