AttributeError 'datetime' module has no attribute 'strptime'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This error almost always comes from confusing the datetime module with the datetime class inside that module. strptime exists, but it is a class method on datetime.datetime, so the fix is usually the right import statement or the right qualified name.
Why the Error Happens
Python’s standard library has a module named datetime. Inside that module there is also a class named datetime.
That means these two imports are not equivalent:
With the first form, you imported the module. With the second form, you imported the class.
So the correct call depends on which style you used.
Use the Matching Call Syntax
If you import the class directly, call datetime.strptime(...).
If you import the module, call datetime.datetime.strptime(...).
The error appears when those two styles get mixed.
Check for Local Module Shadowing
A second common cause is naming your own file datetime.py. In that case, Python imports your local file instead of the standard library module.
If the printed path points into your project instead of the standard library location, rename the file and remove cached bytecode.
After that, rerun the script so Python imports the real library.
Do Not Confuse This With a Format Error
Once the import is fixed, you can still get a different exception if the format string does not match the input. That error is ValueError, not AttributeError.
This distinction matters because the import problem and the date-format problem happen at different stages.
Centralize Parsing Logic
If your project parses dates in several places, wrap that logic in a small helper. That keeps import style consistent and makes it easier to change formats later.
A shared utility also makes tests more focused because parsing behavior is defined in one place.
A Simple Rule for Future Code
The easiest way to avoid this class of bug is to pick one import style for the whole project and stick to it. For example, if a team always writes from datetime import datetime, then every call site consistently uses datetime.strptime(...) and code reviews can spot mistakes quickly.
Consistency matters more than personal preference here. The error is small, but it wastes time because the names look almost identical while referring to different objects.
Common Pitfalls
The biggest mistake is writing import datetime and then calling datetime.strptime(...). That calls the module, not the class.
Another issue is local module shadowing from a file named datetime.py.
A third problem is treating a ValueError from a mismatched format string as if it were the same problem as the missing attribute error.
Summary
- '
strptimebelongs todatetime.datetime, not to the module root.' - '
from datetime import datetimeandimport datetimerequire different call syntax.' - Check for local files named
datetime.pyif attributes seem to be missing. - Separate import problems from format-string problems when debugging.
- Put repeated parsing logic into one helper to keep date handling consistent.

