Select Page

How to Solve Python AttributeError: module ‘datetime’ has no attribute ‘utcnow’

by | Programming, Python, Tips

This error occurs when you import the datetime module and try to call the utcnow() method on the imported module. You can solve this error by importing the datetime class using:

from datetime import datetime

or accessing the class method using:

datetime.datetime.utcnow()

This tutorial will go through the error and how to solve it with code examples.


AttributeError: module ‘datetime’ has no attribute ‘today’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. datetime is a built-in Python module that supplies classes for manipulating dates and times. One of the classes in datetime is called datetime. It can be unclear when both the module and one of the classes share the same name. If you use the import syntax:

import datetime

You are importing the datetime module, not the datetime class. We can verify that we are importing the module using the type() function:

import datetime

print(type(datetime))
<class 'module'>

We can check what names are under datetime using dir() as follows:

import datetime

attributes = dir(datetime)

print('utcnow' in attributes)

In the above code, we assign the list of attributes returned by dir() to the variable name attributes. We can then check for the utcnow() attribute in the list using the in operator. When we run this code, we see it returns False.

False

However, if we import the datetime class using the from keyword and call dir(), we will see utcnow() as an attribute of the class. We can check for utcnow in the list of attributes as follows:

from datetime import datetime

attributes = dir(datetime)

print('utcnow' in attributes)
True

Example

Consider the following example where we want to get the current UTC date and time using utcnow():

import datetime

now = datetime.utcnow()

print(now)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [5], in <cell line: 3>()
      1 import datetime
----> 3 now = datetime.utcnow()
      5 print(now)

AttributeError: module 'datetime' has no attribute 'utcnow'

The error occurred because we imported the datetime module and tried to call the utcnow() method on the module, but utcnow() is an attribute of the datetime class, not the module.

Solution #1: Use the from keyword

We can solve this error by importing the datetime class using the from keyword. Let’s look at the revised code:

from datetime import datetime

now = datetime.utcnow()

print(now)

Let’s run the code to get the result:

2022-05-20 13:18:23.647636

We successfully created a datetime object that contains the current UTC date and time.

Solution #2: Use datetime.datetime

We can also solve this error by importing the module, accessing the datetime class using datetime.datetime, and then calling the utcnow() method. Let’s look at the revised code:

import datetime

now = datetime.datetime.utcnow()

print(now)

Let’s run the code to get the result:

2022-05-20 13:20:41.777953

We successfully created a datetime object that contains the current UTC date and time.

Summary

Congratulations on reading to the end of this tutorial! Remember that from datetime import datetime imports the datetime class and import datetime imports the datetime module.

You may also encounter the error “AttributeError: partially initialized module ‘datetime’ has no attribute ‘utcnow’ (most likely due to a circular import”, which occurs when you have a Python script called datetime.py in your working directory.

Ensure that your local files do not share the same name as built-in or external modules.

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!