How Do You Troubleshoot the Python Problem “TypeError: ‘module’ object is not callable”?

Problem scenario
You run a Python program with datetime, but you get the error "TypeError: 'module' object is not callable." What should you do?

Even though you may use "import datetime" at the top of the program, you should use "from datetime import datetime, timezone" at the top. If you used the syntax "datetime.datetime…", you will need to remove one of the "datetime." strings from the code. Such incorrect syntax will cause an error like "AttributeError: type object 'datetime.datetime' has no attribute 'datetime'" to be thrown.

Here is a program that will not throw any error (as long as you have a file named auth.log in the same directory):

import datetime
from datetime import datetime, timezone
log_reader = open('auth.log', 'r')
for line in log_reader:
    dt_of_log = datetime.strptime(line[:6], '%b %d')
    x = datetime(1900, 6, 15, 15, 17, 8, 132263).isoformat()

Leave a comment

Your email address will not be published. Required fields are marked *