The AttributeError: module ‘datetime’ has no attribute ‘utcnow’ occurs if you have imported the datetime module and directly if we are using the datetime.utcnow()
method on the datetime module.
The datetime
is a module, and it does not have the
method; instead, we need to use the utcnow
()datetime
class name, which has the method correct method and the syntax for the same is datetime.datetime.
utcnow
()
In this tutorial, we will look into what exactly is AttributeError: module ‘datetime’ has no attribute ‘utcnow’ and how to resolve the error with examples.
What is AttributeError: module ‘datetime’ has no attribute ‘utcnow’
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
# print utc datetime
print(datetime.utcnow())
Output
Traceback (most recent call last):
File "c:\Personal\IJS\Code\code22.py", line 5, in <module>
print(datetime.utcnow())
AttributeError: module 'datetime' has no attribute 'utcnow'
In the above example, we are importing the datetime module and trying to print utc Datetime using the datetime.utcnow()
method.
When we run the code, we get an AttributeError: module ‘datetime’ has no attribute ‘utcnow’
The issue occurs because the datetime
module does not have a utcnow()
method, and hence it is throwing an error.
The datetime
module has a class name called datetime
which in turn has the method utcnow()
.
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.
We can also check what are the method available using the dir()
method as shown below.
# import datetime module
import datetime
# print names and methods of datetime
print(dir(datetime))
Output
['MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
'__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI',
'sys', 'time', 'timedelta', 'timezone', 'tzinfo']
If we run the same dir()
method on the datetime.datetime
class we can find all the methods including the utcnow()
# import datetime module
import datetime
# print names and methods of datetime
print(dir(datetime.datetime))
Output
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromisocalendar', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple',
'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
How to resolve AttributeError: module ‘datetime’ has no attribute ‘utcnow’
We can resolve the module ‘datetime’ has no attribute ‘utcnow’ by using the utcnow()
method, which is present inside the datetime
class.
There are two ways to access the utcnow()
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.utcnow()
method.
Syntax
datetime.datetime.utcnow()
Here the first datetime
is a module and the second datetime
is a class which has a method utcnow()
Example –
# import datetime module
import datetime
# print utc datetime
print(datetime.datetime.utcnow())
Output
2022-05-22 07:03:06.015481
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 module
from datetime import datetime
# print utc datetime
print(datetime.utcnow())
Output
2022-05-22 07:03:43.805269
Conclusion
The datetime
module does not have the utcnow()
method; hence if we try to use datetime.utcnow()
directly we get AttributeError: module ‘datetime’ has no attribute ‘utcnow’
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.