AttributeError module 'datetime' has no attribute 'today'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The error "AttributeError module 'datetime' has no attribute 'today'" occurs when we try to call the today method directly on the datetime module. To solve the error, use the following import import datetime and call the today method as datetime.date.today().

attributeerror module datetime has no attribute today

Here is an example of how the error occurs.

main.py
import datetime # ⛔️ AttributeError: module 'datetime' has no attribute 'today' print(datetime.today())

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

To solve the error, call the today method on the date class instead.

main.py
# 👇️ 2022-05-14 print(datetime.date.today()) # 👇️ 2022-05-14 13:45:57.409176 print(datetime.datetime.today())

When called on a date object, the today method returns the current local date.

When called on a datetime object, the today method returns the local datetime.

Make sure you haven't named your local module datetime.py as that would shadow the official datetime module.

Alternatively, you can change your import statement to import the datetime class from the datetime module to not have to type datetime.datetime which looks quite confusing.

main.py
from datetime import datetime, date # 👇️ 2022-05-14 print(date.today()) # 👇️ 2022-05-14 13:45:57.409176 print(datetime.today())

We imported the datetime and date classes from the datetime module and called the today() method on the classes.

It is quite confusing that there is a class named datetime in the datetime module.

You can make your code a bit easier to read by using an alias in your import statement.

main.py
from datetime import datetime as dt, date # 👇️ 2022-05-14 print(date.today()) # 👇️ 2022-05-14 13:45:57.409176 print(dt.today())

We aliased the datetime class to dt, so you would call the today method as dt.today() instead of datetime.today().

The best way to start debugging the "AttributeError module 'datetime' has no attribute 'today'" 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 today, so it must be an attribute in one of the module's classes.

We can also see that the datetime module has an attribute datetime, which is what we used to successfully call the today() method.

If you import the datetime class from the datetime module and pass it to the dir() function, you will see the today method in the list of attributes.

main.py
from datetime import datetime, date # 👇️ [... 'today' ...] print(dir(datetime)) # 👇️ [... 'today' ...] print(dir(date))

If you got the error "AttributeError: partially initialized module 'datetime' has no attribute 'today' (most likely due to a circular import)", there is a file called datetime.py in your local codebase.

To solve the error, make sure to not use names of built-in or remote modules, e.g. datetime.py, for your local files.

Conclusion #

The error "AttributeError module 'datetime' has no attribute 'today'" occurs when we try to call the today method directly on the datetime module. To solve the error, use the following import import datetime and call the today method as datetime.date.today().

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.