AttributeError module 'time' has no attribute 'clock'

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# 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().

main.py
import time start = time.perf_counter() time.sleep(1) end = time.perf_counter() print(end - start) # ๐Ÿ‘‰๏ธ 1.0010583459988993

using time perf counter

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

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().

main.py
import time start = time.process_time() time.sleep(1) end = time.process_time() print(end - start) # ๐Ÿ‘‰๏ธ 3.2690000000001884e-05

using proces time

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).

The perf_counter method:

  • returns the value of the performance counter in fractional seconds.
  • includes time elapsed during sleep and is system-wide.

The process_time method:

  • returns the value of the sum of the system and user CPU time of the process in fractional seconds.
  • does not include time elapsed during sleep and is process-wide.

# If you got the error when working with a third-party module

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.

main.py
# โœ… if you use PyCrypto pip uninstall PyCrypto # ๐Ÿ‘ˆ๏ธ Abandoned project pip install pycryptodome # ๐Ÿ‘ˆ๏ธ Actively maintained fork pip3 uninstall PyCrypto # ๐Ÿ‘ˆ๏ธ Abandoned project pip3 install pycryptodome # ๐Ÿ‘ˆ๏ธ Actively maintained fork # โœ… if you use SQLAlchemy pip install SQLAlchemy --upgrade # ๐Ÿ‘ˆ๏ธ Upgrade package version pip3 install SQLAlchemy --upgrade # ๐Ÿ‘ˆ๏ธ Upgrade package version

If the error is caused by 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.

If none of the suggestions helped, you can try to upgrade all packages in your environment.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
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)
You can store the script in a Python file, e.g. main.py and run the file with python main.py to upgrade all of the outdated packages.

Here are alternative commands you can use to upgrade all outdated packages.

shell
# ๐Ÿ‘‡๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐Ÿ‘‡๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i

If you use a requirements.txt file, you can update it with the following command.

shell
pip freeze > requirements.txt

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev