How to Solve Python TypeError: ‘datetime.datetime’ object is not callable

by | Programming, Python, Tips

The TypeError ‘datetime.datetime’ object is not callable occurs when you try to call a datetime.datetime object by putting parenthesis () after it like a function. Only functions respond to function calls.

This error commonly occurs when you override the name for a built-in class or method from the datetime module.

You can solve the error by avoiding naming variables after class or method names that you want to import.

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


TypeError: ‘datetime.datetime’ object is not callable

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

# Declare function

def simple_function():

    print("Learning Python is fun!")

# Call function

simple_function()
Learning Python is fun!

We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().

Objects of the datetime.datetime class do not respond to function calls because they are not functions. If you try to call a datetime.datetime object as if it were a function, you will raise the TypeError: ‘datetime.datetime’ object is not callable.

We can check if an object is callable by passing it to the built-in callable() method. If the method returns True, then the object is callable. Otherwise, if it returns False the object is not callable. Let’s look at evaluating a datetime.datetime object with the callable method:

from datetime import datetime

today = datetime.today()

print(callable(today))
False

The callable function returns False for the datetime.datetime object.

Example

Let’s look at an example of attempting to call a datetime.datetime object. First, we will import the datetime and date classes from the datetime module and then create a datetime object for today’s date.

from datetime import datetime, date

date = datetime.today()

Next, we will try to create a date object and print the date to the console.

next_week = date(2022, 6, 29)

print(next_week)

The error occurs because we defined a datetime.datetime object and assigned it to the variable name date. Then when we try to create a datetime.date object using the date() constructor, we are instead trying to call the datetime.datetime object called date, which is not callable.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [2], in <cell line: 5>()
      1 from datetime import datetime, date
      3 date = datetime.today()
----> 5 next_week = date(2022, 6, 29)
      7 print(next_week)

TypeError: 'datetime.datetime' object is not callable

Solution

We can solve this error by using variable names not reserved for class or method names which we want to use.

We can find the names of the classes and methods in the datetime module using the dir() method as follows:

import datetime

print(dir(datetime))
['__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']

Let’s look at the revised code:

from datetime import datetime, date

todays_date = datetime.today()

next_week = date(2022, 6, 29)

print(next_week)

We renamed the datetime.datetime object to todays_date allowing us to create the date object using the date() constructor.

Let’s run the code to get the result:

2022-06-29

Summary

Congratulations on reading to the end of this tutorial!

For further reading on not callable TypeErrors, go to the articles:

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

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!