The error “AttributeError: module ‘time’ has no attribute ‘clock'” happens because the function
time.clock() has been removed in Python 3.8 or later. To fix the error, let follow the article below
AttributeError module ‘time’ has no attribute ‘clock’
In Python 3.8 or later
import time print(time.clock())
Traceback (most recent call last): File "./prog.py", line 2, in <module> AttributeError: module 'time' has no attribute 'clock'
How to solve this error?
Use time.time() function
You can replace all statements where the
time.clock() function occurs with the
time.time() function in the code.
- Import module time
time.time()function will return the detailed time to the number of seconds.
import time # The time.time() function will return the detailed time to the number of seconds startTime = time.time() print(startTime) # Stops execution of the current thread within seconds passed. time.sleep(2) # The time.time() function will return the detailed time to the number of seconds. endTime = time.time() print(endTime) print("Elapsed Time: ", endTime - startTime)
1664592364.2982788 1664592366.3102856 Elapsed Time: 2.0120067596435547
Function calculated the elapsed time without program error.
Use functions instead of time.clock()
If you are using Python versions 3.8 and earlier, you can use the following functions to replace the
time.perf_counter() functions provide the same results as the
time.clock() function, which was removed in Python 3.8 and later.
time.perf_counter() function returns the value in fractional seconds of the coefficient counter. It will include the time elapsed during sleep and on the whole system.
import time startTime = time.perf_counter() print(startTime) time.sleep(2) endTime = time.perf_counter() print(endTime) print("Elapsed Time: ", endTime - startTime)
24912350.81560414 24912352.816202506 Elapsed Time: 2.00059836730361
time.process_time() function is a time processing function that does not include I/O operations, network density, and latency in the computation process. The
time.process_time() function returns a float value of the time in seconds. It will not include sleep time like the
import time startTime = time.process_time() for i in range(10): print(i, end=' ') print(startTime) endTime = time.process_time() print(endTime) print("Elapsed Time : ", endTime - startTime)
0 1 2 3 4 5 6 7 8 9 0.016585886 0.016599327 Elapsed Time : 1.3440999999999453e-05
In other words, the
process_time() function calculates the CPU time at the current process.
If you are using the PyCrypto module, I recommend removing it and installing the pycryptodome module. Since the pycryptodome module is more actively supported.
pip3 uninstall PyCrypto pip3 install -U PyCryptodome
Downgrade the version of Python you are using
Downgrading your Python version is probably not the best way to do it, but if the new Python versions make you feel uncomfortable, go for it. It might help you write smoother code.
Maybe you are interested in similar errors:
- AttributeError: ‘dict’ object has no attribute ‘append’
- AttributeError: ‘dict’ object has no attribute ‘has_key’ in Python
- AttributeError: ‘list’ object has no attribute ‘items’ in Python
- AttributeError: ‘list’ object has no attribute ‘split’
- AttributeError: ‘str’ object has no attribute in Python
My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Programming Languages: C++, Python, Java