AttributeError: 'datetime' module has no attribute 'strptime'
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: The Builders
--
Chapters
00:00 Attributeerror: 'Datetime' Module Has No Attribute 'Strptime'
00:31 Answer 1 Score 35
01:12 Accepted Answer Score 620
01:45 Answer 3 Score 5
02:08 Answer 4 Score 3
02:23 Thank you
--
Full question
https://stackoverflow.com/questions/1948...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #class #python27
#avk47
ACCEPTED ANSWER
Score 620
If 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")
ANSWER 2
Score 35
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.
ANSWER 3
Score 5
I got the same problem and it is not the solution that you told. So I changed the "from datetime import datetime" to "import datetime". After that with the help of "datetime.datetime" I can get the whole modules correctly. I guess this is the correct answer to that question.
ANSWER 4
Score 3
Values may differ depending on usage.
import datetime
date = datetime.datetime.now()
date.strftime('%Y-%m-%d') # date variable type is datetime
The value of the date variable must be a string::
date = '2021-09-06'
datetime.datetime.strptime(date, "%Y-%m-%d")
str(datetime.datetime.strptime(date, "%Y-%m-%d")) # show differently