AttributeError: type object ‘datetime.time’ has no attribute ‘time’

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

Happened to run across a very annoying error attempting to generate timestamps tonight in Python. Apparently, the modules datetime and time can conflict with each other so that time.time() will not work if you are also importing the datetime module. I had some suspicion that the bug was due to some sort of bug related to using both datetime and time, but I wasn’t actually the person that figured this out, some wonderful man(woman?) on StackOverflow was! However, I wanted to jot this solution down here on the blog to make sure it is as easy to find as possible– Instead of importing time, import time under an alias such as time_:

# REFERENCE: https://bit.ly/2Gpmoy7
import time as time_
timestamp = int(time_.time())

 

topherPedersen