attributeerror: module time has no attribute clock

What is this error and how does this error occur?

This is a fairly common error message that you may encounter when working with Python, and it can be a bit confusing if you don’t know what’s going on. Basically, this error is telling you that you’re trying to use the time module. The attribute error: module time has no attribute clock pops up when you are trying to access the clock function using the time class object.

 

For Example.

# Here we import the time package
import time

# Here we are getting the AttributeError: module 'time' has no attribute 'clock'
time.clock()

Output:

attributeerror: module time has no attribute clock

 

 

How we can fix attributeerror: module time has no attribute clock?

The function clock() from the time class has been removed, And this has been deprecated since Python 3.3, Now use these functions of the time class time.process_time() or time.perf_counter() instead, depending on your requirements, like this

 

 

time.perf_counter() :  The function perf_counter() it always returns float value in seconds of time. It returns the value in fractional seconds of a performance counter, just like a clock that has the highest available intent to measure a tiny duration.

 

time.process_time(): This function always returns the time in seconds in float value format. And it also returns the value in fractional seconds of the sum of the user CPU time and the system of the current process. Time elapsed is not included during sleep.

 

 

And if you wanna see why they deprecated the time.clock().

 

Also, read Python Tutorials.

 

 

Share this post

Leave a Reply

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