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

by | Programming, Python, Tips

This error occurs when you import the datetime module and try to call the strftime() 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.strftime()

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


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

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('strftime' in attributes)

In the above code, we assign the list of attributes returned by dir() to the variable name attributes. We then check for the strftime() 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 strftime as an attribute of the class. We can check for strftime in the list of attributes as follows:

from datetime import datetime

attributes = dir(datetime)

print('strftime' in attributes)
True

Example

Consider the following example, where we want to get create a string representation of a date and time using the strftime() method.

import datetime

now = datetime.datetime.now()

date_string = datetime.strftime(now, "%m/%d/%Y, %H:%M:%S")

print(date_string)

In the above code, we get the current local date and time using the now() method and then pass the datetime object to strftime() with the format. Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [1], in <cell line: 5>()
      1 import datetime
      3 now = datetime.datetime.now()
----> 5 date_string = datetime.strftime(now, "%m/%d/%Y, %H:%M:%S")
      7 print(date_string)

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

The error occurs because we imported the datetime module and tried to call the strftime() method on the module, but strftime() 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.now()

date_string = datetime.strftime(now, "%m/%d/%Y, %H:%M:%S")

print(date_string)

Note that we had to change the now() method call from datetime.datetime.now() to datetime.now() as we have imported the datetime class. Let’s run the code to see the result:

05/19/2022, 17:49:40

We successfully created a formatted string representing a date and time.

Solution #2: Use datetime.datetime

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

import datetime

now = datetime.datetime.now()

date_string = datetime.datetime.strftime(now, "%m/%d/%Y, %H:%M:%S")

print(date_string)

Let’s run the code to get the result:

05/19/2022, 17:49:40

We successfully created a formatted string representing a 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.

For further reading on AttributeErrors involving datetime, go to the articles:

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!

Follow the Research Scientist Pod on Social media!