The time.clock()
method has been removed in Python 3.8 onwards. Hence if you are using the clock()
method in Python 3.8 or above, you will get AttributeError: module ‘time’ has no attribute ‘clock’.
In this tutorial, we will look into what exactly is AttributeError: module ‘time’ has no attribute ‘clock’ and how to resolve the error with examples.
What is AttributeError: module ‘time’ has no attribute ‘clock’?
The time.clock()
method has been deprecated from Python 3.8 onwards, which leads to an AttributeError: module ‘time’ has no attribute ‘clock’ if used in the code.
If you are not using the clock()
method but still facing this error, that means you are using some of the Python libraries such as PyCrypto, sqlalchemy, etc., that internally use time.clock()
method.
Let us try to reproduce the error in Python 3.8
import time
print(time.clock())
Output
AttributeError: module 'time' has no attribute 'clock'
How to fix AttributeError: module ‘time’ has no attribute ‘clock’?
There are multiple solutions to resolve this AttributeError and the solution depends on the different use cases. Let us take a look at each scenario.
Solution 1 – Replace time.clock() with time.process_time() and time.perf_counter()
Instead of the time.clock()
method, we can use an alternate methods such as time.perf_counter()
and time.process_time()
that provides the same results.
Let us take an example to see how this can be implemented.
The time.perf_counter()
function returns a float value of time in seconds i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide.
import time
# perf_counter includes the execution time and the sleep time
start_time = time.perf_counter()
time.sleep(4)
end_time = time.perf_counter()
print("Elapsed Time is ", end_time-start_time)
Output
Elapsed Time is 4.0122200999903725
The time.process_time()
method will return a float value of time in seconds. The time returned would be the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep.
import time
# perf_counter includes the execution time and the sleep time
start_time = time.process_time()
for i in range(1000):
print(i, end=' ')
end_time = time.process_time()
print("Elapsed Time is ", end_time-start_time)
Output
Elapsed Time is 0.015625
Solution 2- Upgrade the module to the latest version
If you are directly using the time.clock()
method in your code, then some of the external modules that you are using would be using it internally, and hence you are getting this error.
If you use a module like SQLAlchemy, upgrade it to the latest version using the below command.
pip install <module_name> --upgrade
pip install SQLAlchemy --upgrade
Solution 3 – Replace the module with another alternate
If you are using the modules like PyCrypto, there is no way you can upgrade it as it is not maintained actively.
In this case, you need to look for an alternate module like PyCryptodome that supports the same functionality.
# Uninstall the PyCrypto as its not maintained and install pycryptodome
pip3 uninstall PyCrypto
pip3 install pycryptodome
Solution 4 – Downgrade the Python version
It is not a recommended approach to downgrade Python. However, if you have recently upgraded the Python version to 3.8 or above and facing this issue, it is better to revert or downgrade it back to the previous version until you find a solution or implement any of the above solutions.
Conclusion
The AttributeError: module ‘time’ has no attribute ‘clock’ occurs if you are using the time.clock()
method in the Python 3.8 or above version. Another reason could be that you are using an external module that internally uses time.clock()
function.
We can resolve the issue by using alternate methods such as time.process_time()
and time.perf_counter()
methods. If the issue is caused due to external modules, it is better to upgrade the module or find an alternative module if the module provides no fix.