Attributeerror: module time has no attribute clock occurs because of time.clock function is now deprecated in python 3.8 or later versions. Now if you are using the latest version and running the older syntax, you will get the same error. Now, what is the solution? There are two approaches to fix this issue. The First is downgrading the python version so that it supports the same syntax. The second is changing our code base with the Alternative syntax. Well in this article we are going to try both of them.
Attributeerror: module time has no attribute clock ( Solution ) –
Firstly let’s discuss the alternative syntax approach.
Approach 1: Tring Alternatives for deprecated syntax –
Case 1: Using time. time as an alternative –
Try time.time in the place of time.clock as the quickest solution. Either you can try at the beginning like this –
or you need to change it specifically at each caller statement.
Case 2: Specific to PyCrypto module –
Secondary sometimes it is because of PyCrypto module then the first thing you should try is to uninstall the same and install PyCryptodome module as a replacement.
pip3 uninstall PyCrypto pip3 install -U PyCryptodome
Case 3: Using time.perf_counter() or time.process_time() as alternative –
We can also use time.perf_counter() or time.process_time() as an alternative for time.clock. Actually this time.process_time() is the processor timimg which do not include I/O operations, delay , network letancy etc while calculation process time. But time.perf_counter() includes all such components.
Approach 2: Downgrading Python –
This is quite straightforward. If we are not bound to stick with any specific python version then downgrading is the easiest option to fix this issue. Go with a lower version of python like ( 3.7 or less ). Then you will not get this error.
Some of us would be thing think why time.clock is deprecated? Actually, it returns different results with different OS like ( Unix, Windows, etc) . It means it was Platform dependent function. I hope the reason and fix is clear to everyone.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.