Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module 'time' has no attribute 'clock' in Python 3.8

People also ask

What is time module in Python?

The Python time module provides many ways of representing time in code, such as objects, numbers, and strings. It also provides functionality other than representing time, like waiting during code execution and measuring the efficiency of your code.


From the Python 3.8 doc:

The function time.clock() has been removed, after having been deprecated since Python 3.3: use time.perf_counter() or time.process_time() instead, depending on your requirements, to have well-defined behavior. (Contributed by Matthias Bussonnier in bpo-36895.)


Check if you are using PyCrypto, if yes, uninstall it and install PyCryptodome which is a fork of PyCrypto

PyCrypto is dead as mentioned on project issue page

Since both these libraries can coexist, it could be an issue too

pip3 uninstall PyCrypto
pip3 install -U PyCryptodome

time.clock() was removed in 3.8 because it had platform-dependent behavior:

  • On Unix, this returns the current processor time (in seconds)

  • On Windows, this returns wall-clock time (in seconds)

    # I ran this test on my dual-boot system as demonstration:
    print(time.clock()); time.sleep(10); print(time.clock())
    # Linux:            # Windows:
    # 0.0382            # 26.1224
    # 0.0384            # 36.1566
    

So which function to pick instead?

  • Processor Time: This is how long this specific process spends actively being executed on the CPU. Sleep, waiting for a web request, or time when only other processes are executed will not contribute to this.

    • Use time.process_time()
  • Wall-Clock Time: This refers to how much time has passed "on a clock hanging on the wall", i.e. outside real time.

    • Use time.perf_counter()

      • time.time() also measures wall-clock time but can be reset, so you could go back in time
      • time.monotonic() cannot be reset (monotonic = only goes forward) but has lower precision than time.perf_counter()

Reason: time.clock is deprecated

Solution:

Step 1

  • Just go to your C folder and search site-packages in the search bar. You will see the result like this:

site-packages

  • Right click on this result marked as Red, and open file location.

  • Then look for sqlalchemy/util/compat.py file and open it. And search time.clock using ctrl+f and replace it with time.time

Step 2

  • Go to your error last line and go to that directory and open that particular file.
  • search time.clock using ctrl+f and replace it with time.time

Go to the the code C:\Users\Mr\anaconda3\envs\pythonProject2\Lib\site-packages\sqlalchemy\util and select compat.py and search for time.clock in code.

Then replace time.clock with time.time and save it.


AttributeError: module 'time' has no attribute 'clock' 

It is deprecated as said which means just use the latest versions of libraries that have that module. For example, depending on the dependency you have, Remove and Install

Crypto==1.4.1, or Mako==1.1.2 or SQLAlchemy==1.3.6 //etc

The idea is you don't have to downgrade your python version as this will catch up with you later. Just update the packages to more late ones which are compatible with Python 3.8