Skip to main content

How to Solve "AttributeError: module 'time' has no attribute 'clock'" in Python

The error "AttributeError: module 'time' has no attribute 'clock'" occurs because the time.clock() function was removed in Python 3.8.

This guide explains the cause of the error and provides the correct replacements: time.perf_counter() and time.process_time().

Understanding the Error: time.clock() Removal

Before Python 3.8, time.clock() was used (primarily on Windows) to measure processor time. However, its behavior was platform-dependent and often confusing. Therefore, it was removed in Python 3.8. Attempting to use it in Python 3.8+ results in the AttributeError.

Replacement 1: time.perf_counter() (Wall-Clock Time)

time.perf_counter() is the recommended replacement for measuring elapsed time (wall-clock time). It provides the highest-resolution timer available on your system:

import time

start = time.perf_counter() # Get the starting time

time.sleep(1) # Simulate some work

end = time.perf_counter() # Get the ending time
elapsed_time = end - start # Calculate the elapsed time

print(elapsed_time) # Output: 1.0010583459988993 (approximate)
  • time.perf_counter(): Returns a float representing the number of seconds since some unspecified, but fixed, point in time. The absolute value is meaningless; only the difference between two calls is useful.
  • Includes sleep time: perf_counter() measures elapsed time, including time spent sleeping (using time.sleep(), waiting for network I/O, etc.).

Replacement 2: time.process_time() (CPU Time)

time.process_time() measures the CPU time used by the current process. This excludes time spent sleeping:

import time

start = time.process_time()

time.sleep(1) # Simulate some work

end = time.process_time()
elapsed_cpu_time = end - start

print(elapsed_cpu_time) # Output: (a small value close to 0, e.g., 3.2690000000001884e-05)
  • This is a method to measure the CPU time that process takes.
  • The time does not include time elapsed during sleep, because in that case, CPU is not used.

Choosing the Right Replacement

  • time.perf_counter(): Use this for measuring elapsed time, including any time spent waiting (e.g., for network requests, user input, or time.sleep()). This is the most common use case and is almost always what you want when you were previously using time.clock().
  • time.process_time(): Use this for measuring CPU time used by your code, excluding sleep time. This is useful for profiling and benchmarking the performance of your code itself.

Handling the Error in Third-Party Modules

If you encounter this error in a third-party library (not in your own code), you have a few options:

  • Update the library: The best solution is to update the library to a newer version that no longer uses time.clock(). Use pip install --upgrade <library_name>.
  • Find an alternative library: If the library is unmaintained or the update doesn't fix the issue, consider using a different library that provides similar functionality.
  • Downgrade Python (Not Recommended): As a last resort, you could downgrade to Python 3.7 or earlier. This is strongly discouraged, as you'll miss out on security updates, bug fixes, and new features. It's much better to fix the underlying issue in the library.

Upgrading all packages (last resort)

If the problem is caused by an outdated package you can run:

import pkg_resources
from subprocess import call

packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)
  • This will upgrade all of the outdated packages, but should be only used as a last resort.

Conclusion

The AttributeError: module 'time' has no attribute 'clock' error is caused by using the removed time.clock() function in Python 3.8 or later.

  • The correct replacements are time.perf_counter() for measuring elapsed time and time.process_time() for measuring CPU time.
  • If you encounter this error in a third-party library, update the library or find an alternative.
  • Avoid downgrading Python, as this is not a sustainable solution.