[Solved] Attributeerror: module ‘time’ has no attribute ‘clock’

When you are working with the time module, you might face Attributeerror: module ‘time’ has no attribute ‘clock’ error which occurs when because Python 3.8 has removed the clock module. To solve the error, we can use time.perf_counter() or time.process_time() function. In this short article, we will learn various methods to solve Attributeerror: module ‘time’ has no attribute ‘clock’ error. Moreover, we will discuss how to solve such errors and understand errors in Python.

Attributeerror: module 'time' has no attribute 'clock'-error

Attributeerror: module ‘time’ has no attribute ‘clock’ – Possible Solutions

Attributeerror: module ‘time’ has no attribute ‘clock’ error occurs because the clock() method is no more available in Python > 3.8 version. For example, let us say we want to get the time right now using clock() method.

# importing the time module
import time

# starting time
start = time.clock()

# sleep time
time.sleep(1)

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_211002/1892036945.py in <module>
      3 
      4 # starting time
----> 5 start = time.clock()
      6 
      7 # sleep time

AttributeError: module 'time' has no attribute 'clock'

As you can see, we get AttributeError: module ‘time’ has no attribute ‘clock’ error because the clock method is not available.

In this short article, we will solve the error using the following methods:

  1. Using time.perf_counter() method
  2. Using process_time() method
  3. Uninstalling external modules
  4. Using time.time alternatively

Let us now solve the error using the mentioned methods and explained each method.

Solution-1: Using time.perf_counter() method

We can use the time.perf_counter() method to get rid of the error if you are using a Python version greater than 3.8. The float value of time in seconds is always returned by the perf counter() method. Return the value of a performance counter, or a clock with the maximum attainable resolution to measure a brief duration, in fractional seconds. It is system-wide and does take into account sleep time.

Let us see how we can use the time.perf_counter() method to solve the error:

# importing the time module
import time

# starting time
start = time.perf_counter()

# sleep time
time.sleep(1)

# printing
print(start)

Output:

211390.432776234

As you can see, this time we didn’t get AttributeError: module ‘time’ has no attribute ‘clock’ error.

Solution-2: Using process_time() method

In Python versions greater than 3.8, we can also process_time() to get rid of AttributeError: module ‘time’ has no attribute ‘clock’ error. The float value of time in seconds is always returned by the process_time() function. Return the total amount of system and user CPU time used by the current process, expressed in fractional seconds. It excludes the amount of time spent sleeping.

Let us take an example and see how we can use this method:

# importing the time module
import time

# starting time
start = time.process_time()

# sleep time
time.sleep(1)

# printing
print(start)

Output:

0.765500057

As you can see, we didn’t get any errors this time.

Solution-3: Uninstalling external modules

Suppose you are not using time.clock() method in your script but still are getting this error then most probably you are using an external module that uses the clock() method. To solve it, either you have to update the modules or replace them.

Usually, this error also occurs because of using an unmaintained pycrypto module or having an older version sqlalchemy.

First, you need to uninstall the PyCrypto and install PyCrptodome using the following commands:

# if you use PyCrypto
# uninstall module
pip3 uninstall PyCrypto

# install module 
pip3 install pycryptodome

You can also upgrade sqlalchmey module if you are facing an error because of that module.

# if you use SQLAlchemy
pip3 install SQLAlchemy --upgrade

# alternative 
pip install --upgrade flask_sqlalchemy

As you can see, we didn’t get the error now.

Solution-4: Using time.time alternatively

Another way to eliminate the error is to use time.time as an alternative.

# alternative method
time.clock = time.time
Attributeerror: module 'time' has no attribute 'clock'-solved

This should also help you to get rid of the error.

Understanding the error

The error in Python gives more information about the error. The first part of the error shows the category of the error which in this case was AttributeError. The second part of the error gives more information about the error and tells us which part of the error or method gives the error.

What is AttributeError in Python?

When you try to access or call an attribute that an object doesn’t have, Python throws an AttributeError. You will receive an AttributeError, for instance, if you create an instance of the class Dog and try to access an age attribute that the instance does not have because the class Dog has a name attribute. Make sure the object contains the attribute you are attempting to access in order to resolve this issue or use the hasattr() function to determine whether the object has the attribute before attempting to access it.

Summary

In this short article, we learned how we can solve Attributeerror: module ‘time’ has no attribute ‘clock’ error. We learned 4 different methods to solve Attributeerror: module ‘time’ has no attribute ‘clock’ error. Moreover, we also discuss how we can solve such errors in the future.

Related Issues

2 thoughts on “[Solved] Attributeerror: module ‘time’ has no attribute ‘clock’”

  1. Pingback: ModuleNotFoundError: No module named 'rest_framework' [Solved] - Techfor-Today

  2. Pingback:  ImportError: No Module named Setuptools [Solved] - Techfor-Today

Leave a Comment

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

Scroll to Top