Last updated: Apr 8, 2024
Reading timeยท3 min
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.
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 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()
.
import time start = time.process_time() time.sleep(1) end = time.process_time() print(end - start) # ๐๏ธ 3.2690000000001884e-05
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.
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:
The process_time
method:
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 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.
The most straightforward way to upgrade all outdated packages is to use a Python script.
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)
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.
# ๐๏ธ 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.
pip freeze > requirements.txt
You can learn more about the related topics by checking out the following tutorials: