120

I have written code to generate public and private keys. It works great at Python 3.7 but it fails in Python 3.8. I don't know how it fails in the latest version. Help me with some solutions.

Here's the Code:

from Crypto.PublicKey import RSA


def generate_keys():
    modulus_length = 1024
    key = RSA.generate(modulus_length)
    pub_key = key.publickey()
    private_key = key.exportKey()
    public_key = pub_key.exportKey()
    return private_key, public_key


a = generate_keys()
print(a)

Error in Python 3.8 version:

Traceback (most recent call last):
  File "temp.py", line 18, in <module>
    a = generate_keys()
  File "temp.py", line 8, in generate_keys
    key = RSA.generate(modulus_length)
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/PublicKey/RSA.py", line 508, in generate
    obj = _RSA.generate_py(bits, rf, progress_func, e)    # TODO: Don't use legacy _RSA module
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/PublicKey/_RSA.py", line 50, in generate_py
    p = pubkey.getStrongPrime(bits>>1, obj.e, 1e-12, randfunc)
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Util/number.py", line 282, in getStrongPrime
    X = getRandomRange (lower_bound, upper_bound, randfunc)
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Util/number.py", line 123, in getRandomRange
    value = getRandomInteger(bits, randfunc)
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Util/number.py", line 104, in getRandomInteger
    S = randfunc(N>>3)
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 202, in read
    return self._singleton.read(bytes)
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 178, in read
    return _UserFriendlyRNG.read(self, bytes)
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 129, in read
    self._ec.collect()
  File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 77, in collect
    t = time.clock()
AttributeError: module 'time' has no attribute 'clock'
Smack Alpha
  • 1,828
  • 1
  • 17
  • 37

16 Answers16

123

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.)

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
Angus
  • 3,680
  • 1
  • 12
  • 27
105

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
Himanshu Sheoran
  • 1,266
  • 1
  • 6
  • 5
74

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:    0.0382 --------------------------->  0.0384
    # Windows: 26.1224 ---------------------------> 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()
xjcl
  • 12,848
  • 6
  • 67
  • 89
  • Thank you for the information. I have installed `chatterbot` and obtained the error message. I will try to downgrade to Python 3.7. – dudung Feb 26 '23 at 00:11
21

Necroposting this ugly monkey patch:

import time
time.clock = time.time

Works as a last resort, but not recommended.

fviktor
  • 2,861
  • 20
  • 24
19

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
SARIM
  • 992
  • 3
  • 14
  • 22
5

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.

SHR
  • 7,940
  • 9
  • 38
  • 57
  • 2
    Does this apply to OP's situation? Is modifying the code of a library like this really a good idea? – AMC Oct 29 '20 at 00:12
  • although it may not the right and appropriate answer but that answer worked for me since I had a little experience with python – Flowra Nov 26 '20 at 02:23
4
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

joash
  • 2,205
  • 2
  • 26
  • 31
3

The module you use to generate key call a method that have been depreciated since python 3.3 time.clock().

You could downgrade to python 3.7 or change the source code to replace it. You should open an issue for that as well.

Florian Bernard
  • 2,561
  • 1
  • 9
  • 22
3

time.clock is used in many old libraries. Now that time.clock is removed, one must click on the path given in the error. This will navigate you to the line where time.clock is written, and just change it to time.time.

2

Open the file and go to the line as pointed in the error message, change the time.clock() line to time.perf_counter(), if it still doesn't work modify it to time.time

1

I had the same issue for encrypting a string using AES in my project neehack.com and I fixed it by updating venv/lib/python3.8/site-packages/Crypto/Random/_UserFriendlyRNG.py line 77 to t = time.time() and it is now fixed.

MorariJan
  • 35
  • 3
  • `time.time()` and `time.clock()` are not compatible. You should replace it with `time.perf_counter()` instead which behaves in a more similar way – mousetail Jan 27 '21 at 15:29
1

In my case I replace time.clock() with time.time()

Zia Khan
  • 188
  • 2
  • 9
0

If you have database involved, upgrade it.

pip install --upgrade flask_sqlalchemy
Jefferson Sankara
  • 715
  • 1
  • 5
  • 7
0

Just open File "/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 77

and change from

 t = time.clock()

to

 t = time.time
0

Open file that debug error line. And change t = time.clock() to t = time.time

Kairat Koibagarov
  • 1,385
  • 15
  • 9
0

testing on python3.6 env :

>>> print("%s %s" % (time.process_time(), time.clock()))
>>> 0.288050639 0.288056

that mean process_time is what the lib require.

firebird631
  • 569
  • 7
  • 10