The error type object ‘datetime.datetime’ has no attribute ‘datetime’ usually comes from importing or calling datetime incorrectly in Python code.
What This Datetime Attribute Error Really Means
The message above looks strange on first sight. Python is telling you that you are asking for an attribute named datetime on something that is already a datetime.datetime class.
In plain terms, Python thinks you are holding the class itself, not the datetime module. When you write datetime.datetime(...), Python expects the first part to be the module and the second part to be the class inside that module. If the first part is already the class, the extra .datetime no longer exists, so Python raises an AttributeError.
That message format tells you a lot. The words type object mean Python is working with a class, not an instance. The phrase has no attribute means the attribute look up failed; the class simply does not provide the attribute name that you requested.
If you run a short check in a REPL, you can see this in action. Run from datetime import datetime, then type datetime on its own. The console prints something like , which confirms that the name now points at the class itself, not the module that holds it. Trying datetime.datetime(2024, 5, 1) in that state reproduces the error at once.
Once you see that pattern, the message stops feeling mysterious. Any time you notice a type name repeated twice, ask whether the first part should be a module name instead. That habit alone steers you toward the section of the code base that changed the import or the variable binding. That small step saves time during debugging sessions.
This often happens in code that mixes import datetime with from datetime import datetime, or when a local variable or file name shadows the standard library module. The fix is to check what the name datetime points to before you call it.
Where The ‘Datetime.Datetime’ Object Has No ‘Datetime’ Attribute Error Shows Up
This error appears in many small scripts and larger projects. It tends to surface when dates are created or parsed, often in a single line that looks harmless.
- Creating a fixed date — Code like
date = datetime.datetime(2024, 5, 1)with the wrong import pattern can trigger the error. - Calling
now()— Lines such asnow = datetime.datetime.now()fail whendatetimerefers to the class rather than the module. - Parsing strings — A call such as
parsed = datetime.datetime.strptime(raw, '%Y-%m-%d')breaks under the same conditions. - Inside larger tools — Logs from libraries such as App Engine, Raspberry Pi helpers, or desktop launchers may show the same message when a helper module handles datetime in an unexpected way.
Every one of these situations shares the same pattern: the name datetime no longer points where you expect.
Developers often meet this message while reading logs on a server or during a notebook run. The line that fails may sit inside a helper function written months ago. Once you know that the core problem is a mixed import or a shadowed name, you can scan the imports at the top of the file and the recent edits around date handling first, instead of chasing unrelated parts of the code.
This error also crops up when code jumps between environments. A script that runs in a local shell might behave differently inside a scheduled job or a hosted notebook where other startup files pre load modules. If the shared environment imports datetime in a different way, your own assumptions about the name can suddenly break.
Why The ‘Datetime.Datetime’ Object Has No ‘Datetime’ Attribute Error Happens
The root cause is almost always a naming issue. Python resolves names by walking through scopes and modules. If anything in that chain reuses the name datetime, the standard module can quietly disappear from view.
- Mixing import styles — Using
import datetimein one file andfrom datetime import datetimein another makes it easy to forget which one is active. The second form binds the namedatetimeto the class, not the module, sodatetime.datetimebecomes invalid. - Reassigning the name — Code such as
datetime = datetime.datetime.now()replaces the reference to the class or module with a specific value. Any later call todatetime.datetimethen fails. - File named datetime.py — A script or module called
datetime.pyin your project folder can mask the built in module when Python builds the import path. The interpreter loads your file instead of the standard module. - Third party scripts — Add on packages or custom startup scripts sometimes import datetime in their own way. When a project pulls those scripts in, they might set
datetimeto a different object, which then leaks into shared namespaces.
Once you understand that datetime is just a name that can point to a module, class, or instance, the error message starts to make sense.
Python resolves names by looking first in the local scope, then in any enclosing scopes, then in the module, and only then in the built in namespace. A re used name at any of those levels wins over the standard module. That is why a simple assignment such as datetime = '2024-05-01' in a function can break a clean import that sits at the top of the file.
Fixing Type Object ‘Datetime.Datetime’ Has No Attribute ‘Datetime’ In Python Code
You can clear the error by restoring a clean relationship between the datetime module and the datetime class, then avoiding name collisions in your code.
- Pick one import style — Use either
import datetimeorfrom datetime import datetime, not both in the same scope. The first style keeps the module and the class clearly separate; the second style gives you a shorter call syntax. - Match calls to imports — If you use
import datetime, stick todatetime.datetime(...). If you usefrom datetime import datetime, calldatetime(...)directly. - Check for shadowing — Search your code for lines that assign to
datetime. Any variable, parameter, or attribute with that name can hide the module or class. - Rename local files — If you have a file called
datetime.pyor a package directory calleddatetime, rename it and remove any.pycor__pycache__entries that refer to the old name. - Restart interactive sessions — In notebooks and REPL sessions, restart the kernel so that stale bindings for
datetimedisappear and fresh imports take effect.
Step By Step Debug Session
When the message appears, walk through a short routine. Start by printing datetime and type(datetime) near the failing line. If the output shows a class or a plain string instead of a module, you have confirmed a naming conflict.
Next, scan the top of the file for imports and match each call pattern to an import style. Then search downward for assignments to datetime. A text search inside your editor is often enough. Any conflicting line is a candidate for a rename.
In larger projects, check whether your package structure includes a folder or file named datetime. A quick look at the directory tree or a call to datetime.__file__ in a Python shell reveals the actual source of the module that was imported.
Once these steps are in place, a fresh run of the script should no longer show the error.
Practical Examples With Correct Datetime Usage
Seeing working code helps fix the mental model. The snippets below show common date tasks written in safe patterns that avoid the type object ‘datetime.datetime’ has no attribute ‘datetime’ message.
Example 1: Using The Datetime Module Name
import datetime
# create a specific date
start_of_month = datetime.datetime(2024, 5, 1)
# current date and time
now = datetime.datetime.now()
# parsing from a string
parsed = datetime.datetime.strptime("2024-05-01", "%Y-%m-%d")
Here the name datetime always refers to the module. The class lives inside that module as datetime.datetime, so the chained calls are valid.
Example 2: Importing The Datetime Class Directly
from datetime import datetime
# create a specific date
start_of_month = datetime(2024, 5, 1)
# current date and time
now = datetime.now()
# parsing from a string
parsed = datetime.strptime("2024-05-01", "%Y-%m-%d")
In this pattern the name datetime holds the class itself. That is why a call such as datetime.datetime(...) would raise the error: Python would see a class, not a module, at the first step.
Both approaches are valid. Many engineers choose import datetime in most files, then keep from datetime import datetime for short scripts or notebooks where typing the full path feels noisy. Consistency inside a single file or module helps a future reader trust what the name datetime represents.
Wrong Vs Correct Datetime Imports
| Scenario | Problem Code | Fixed Code |
|---|---|---|
| Class imported then called as module | from datetime import datetimets = datetime.datetime.now() |
from datetime import datetimets = datetime.now() |
| Module shadowed by variable | import datetimedatetime = datetime.datetime.now() |
import datetimecurrent = datetime.datetime.now() |
| Local file masks standard module | datetime.py in project folder |
Rename file to dates_util.py and delete cache files |
How To Prevent Datetime Attribute Errors In Future Projects
A small habit change in imports and naming keeps this issue away in long lived code bases.
Clear style rules belong in project docs or a short CONTRIBUTING file. When new team members see sample snippets that already follow the chosen pattern, they are less likely to introduce a second style that re uses the same names in a conflicting way.
- Standardise imports — Pick a project wide rule for the datetime module. Many teams prefer
import datetimeand always write the fulldatetime.datetimeordatetime.datepath. That style avoids collisions between the module and the class. - Avoid generic names — When storing timestamps, use variable names such as
created_at,updated_at, orrun_time. Leave the plain worddatetimefree for the module or class only. - Watch imports in shared scripts — In tools, startup hooks, or custom shells, follow the same import style. That way, the name that reaches your main code behaves in a predictable way across environments.
- Log object types during debugging — When an attribute error appears, drop in a quick
print(type(datetime))near the failing line. The output immediately shows whether you are dealing with the module, the class, or a stray value.
Static analysis tools can help here as well. Linters that flag unused imports, duplicate names, or shadowed built in objects will often warn you when a local variable hides a standard module. Running those tools as part of your test run gives you an early signal before the bug reaches production logs.
Once your project treats the word datetime with care, the message type object ‘datetime.datetime’ has no attribute ‘datetime’ turns from a frustrating surprise into a quick hint about where a name went off track.
