AttributeError:模块“时间”在Python 3.8中没有属性“时钟”

5 python attributeerror pycrypto python-3.8

我已经编写了生成公共和私有密钥的代码。它在Python 3.7上运行良好,但在Python 3.8中失败。我不知道在最新版本中它怎么会失败。为我提供一些解决方案。

这是代码:

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)
Run Code Online (Sandbox Code Playgroud)

Python 3.8版本中的错误:

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'
Run Code Online (Sandbox Code Playgroud)

Ang*_*Tay 83

来自Python 3.8 文档

time.clock()自 Python 3.3 起已被弃用后,该函数已被删除:使用time.perf_counter()time.process_time()代替,根据您的要求,具有明确定义的行为。(由 Matthias Bussonnier 在bpo-36895 中提供。)


小智 60

检查您是否正在使用 PyCrypto,如果是,请卸载它并安装PyCryptodom,它是PyCrypto的一个分支

项目问题页面所述,PyCrypto 已死

由于这两个库可以共存,这也可能是一个问题

pip3 uninstall PyCrypto
pip3 install -U PyCryptodome
Run Code Online (Sandbox Code Playgroud)


xjc*_*jcl 42

time.clock()在 3.8 中删除,因为它具有平台相关的行为

  • Unix 上,这将返回当前处理器时间(以秒为单位)

  • Windows 上,这将返回挂钟时间(以秒为单位)

    # 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
    
    Run Code Online (Sandbox Code Playgroud)

那么该选择哪个函数呢?

  • 处理器时间:这是此特定进程在 CPU 上主动执行所花费的时间。睡眠、等待 Web 请求或仅执行其他进程的时间不会对此产生影响。

    • time.process_time()
  • 挂钟时间:这是指“挂在墙上的时钟”已经过去了多少时间,即在实时之外。

    • time.perf_counter()

      • time.time() 还可以测量挂钟时间,但可以重置,因此您可以回到过去
      • time.monotonic() 无法重置(单调 = 只前进)但精度低于 time.perf_counter()


fvi*_*tor 21

死贴这个丑陋的猴子补丁:

import time
time.clock = time.time
Run Code Online (Sandbox Code Playgroud)

作为最后的手段,但不推荐。


小智 5

转到代码 C:\Users\Mr\anaconda3\envs\pythonProject2\Lib\site-packages\sqlalchemy\util 并选择 compat.py 并time.clock在代码中搜索。

然后替换time.clocktime.time并保存。

  • 这适用于OP的情况吗?像这样修改库的代码真的是个好主意吗? (2认同)

Cha*_*rab 5

原因:time.clock 已弃用

解决方案:

第1步 -

  • 只需转到您的C 文件夹并在搜索栏中搜索站点包。你会看到这样的结果。

站点包

  • 右键单击标记为红色的此结果,然后打开文件位置

  • 然后查找sqlalchemy/util/compat.py文件并打开它。并使用 ctrl+f搜索time.clock并将其替换为time.time

第2步 -

  • 转到您的错误最后一行并转到该目录并打开该特定文件。
  • 使用 ctrl+f搜索time.clock并将其替换为time.time

希望,你会发现它有帮助。点赞。快乐编码。


Flo*_*ard 3

用于生成密钥的模块调用自 python 3.3 time.clock()以来已弃用的方法。

您可以降级到 python 3.7 或更改源代码来替换它。您也应该为此提出一个问题。