AttributeError module 'time' has no attribute 'clock'


Borislav Hadzhiev

Wed Apr 20 20222 min read

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

The Python "AttributeError module 'time' has no attribute 'clock'" occurs because since Python v3.8, the clock() function has been removed. To solve the error, use the time.perf_counter() or time.process_time() functions instead.

attributeerror module time has no attribute clock

Here is an example that uses perf_counter().
import time start = time.perf_counter() time.sleep(1) end = time.perf_counter() print(end - start) # 👉️ 1.0010583459988993

The clock() function has been removed in Python 3.8, so we have to use perf_counter or process_time.

Here is an example that uses process_time().
import time start = time.process_time() time.sleep(1) end = time.process_time() print(end - start) # 👉️ 3.2690000000001884e-05

The perf_counter function returns the number of fractional seconds of a performance counter. The function includes time elapsed during sleep.

The process_time function returns the number of fractional seconds of the system and user CPU time of the current process. The function doesn't include time elapsed during sleep.

The result from the process_time function represents the active time a process is being executed on the CPU, whereas the result from the perf_counter method represents how much time has passed (including sleep time, waiting on web requests, etc).

If you didn't use the time.clock() function but still got the error, then you are using an external module that uses the clock() function and you have to update it or replace it if it isn't maintained anymore.

Most commonly the error is caused by using the unmaintained PyCrypto module or having an older version of sqlalchemy installed.
# ✅ if you use PyCrypto pip3 uninstall PyCrypto # 👈️ abandoned project pip3 install pycryptodome # 👈️ actively maintained fork # ✅ if you use SQLAlchemy pip install SQLAlchemy --upgrade # 👈️ upgrade package version

If the error is caused from another external module, try upgrading it with the pip install your_module --upgrade command.

If the module wasn't updated to not use the clock() function, you have to look for a replacement or use a Python version lower than 3.8.

Use the search field on my Home Page to filter through my more than 1,000 articles.