AttributeError module 'datetime' has no attribute 'strftime'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The error "AttributeError module 'datetime' has no attribute 'strftime'" occurs when we try to call the strftime method directly on the datetime module. To solve the error, create a datetime object and call the strftime method on the object instead.

attributeerror module datetime has no attribute strftime

Here is an example of how the error occurs.

main.py
import datetime # ⛔️ AttributeError: module 'datetime' has no attribute 'strftime' print(datetime.strftime("%d/%m/%y"))

The issue in the code sample is that we are trying to call the strftime method directly on the datetime module.

To solve the error, we have to create a datetime object first and call the strftime method on the object.

main.py
import datetime d = datetime.datetime(2022, 11, 24, 9, 30, 0) # 👇️ 24/11/22 print(d.strftime("%d/%m/%y")) # 👇️ ️Thursday, 24. November 2022 09:30AM print(d.strftime("%A, %d. %B %Y %I:%M%p")) # 👇️ Saturday, 14. May 2022 11:40AM d2 = datetime.datetime.now() # 👉️ current date print(d2.strftime("%A, %d. %B %Y %I:%M%p"))
Make sure you haven't named your local module datetime.py as that would shadow the official datetime module.

We imported the datetime module and instantiated the datetime class.

The datetime class has a strftime method which we can use to get a string representation of the date and time, controlled by an explicit format string.

It is quite confusing that the datetime module has a datetime class.

An alternative to chaining datetime.datetime, is to import the class from the module.

main.py
from datetime import datetime d = datetime(2022, 11, 24, 9, 30, 0) # 👇️ 24/11/22 print(d.strftime("%d/%m/%y")) # 👇️ ️Thursday, 24. November 2022 09:30AM print(d.strftime("%A, %d. %B %Y %I:%M%p")) # 👇️ Saturday, 14. May 2022 11:40AM d2 = datetime.now() print(d2.strftime("%A, %d. %B %Y %I:%M%p"))

We imported the datetime class from the datetime module, so we don't have to use datetime.datetime when instantiating the class.

You can also use an alias in your import statement.

main.py
from datetime import datetime as dt d = dt(2022, 11, 24, 9, 30, 0) # 👇️ 24/11/22 print(d.strftime("%d/%m/%y")) # 👇️ ️Thursday, 24. November 2022 09:30AM print(d.strftime("%A, %d. %B %Y %I:%M%p")) # 👇️ Saturday, 14. May 2022 11:40AM d2 = dt.now() print(d2.strftime("%A, %d. %B %Y %I:%M%p"))

We aliased the datetime class to dt, so we can access methods on the class as dt.now() instead of datetime.now().

The best way to start debugging the "AttributeError module 'datetime' has no attribute 'strftime'" is to call the dir() function passing it the imported module.

main.py
import datetime """ [ 'MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo' ] """ print(dir(datetime))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the datetime module has no attribute named strftime, so it must be an attribute in one of the module's classes.

However, if we pass the datetime object to the dir() function, we can see that it has an attribute strftime.

main.py
import datetime d = datetime.datetime(2022, 11, 24, 9, 30, 0) # [... 'strftime' ...] print(dir(d))

The dir() function is very useful when you need to look at what exactly you are importing and which attributes are available on the imported module.

Conclusion #

The error "AttributeError module 'datetime' has no attribute 'strftime'" occurs when we try to call the strftime method directly on the datetime module. To solve the error, create a datetime object and call the strftime method on the object instead.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.