Python 3.8中出现"AttributeError: module 'time' has no attribute 'clock'"错误

121

我编写了生成公钥和私钥的代码。在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)

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'

在这里引用了Crytpo库:https://github.com/dlitz/pycrypto/issues/283 但是这个问题好像已经一年没有关闭了。 - Fabien Antoine
错误出现在一个12行程序的第18行。请展示产生错误的代码。 - ctrl-alt-delor
17个回答

124

来自Python 3.8文档

time.clock()函数已被移除,此函数自Python 3.3版本起就已被弃用:根据您的需求使用time.perf_counter()time.process_time()代替,以获得明确定义的行为。(由Matthias Bussonnier在bpo-36895中提供。)


106

检查一下你是否在使用PyCrypto,如果是,请卸载它并安装PyCryptodome,它是PyCrypto的一个分支

正如在项目问题页面上所提到的那样,PyCrypto已经死亡了

由于这两个库可以共存,所以可能会出现问题

pip3 uninstall PyCrypto
pip3 install -U PyCryptodome

如果您遇到权限错误,请使用--user而不是-U - harrydemuth
如果在运行 gsutil rsync 时出现此错误,这也是解决方案。请参见 此链接 - RunOrVeith

75

time.clock() 在3.8中被移除了,因为它具有平台相关行为:

  • 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
    

那么应该选择哪个函数呢?

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

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

    • 使用 time.perf_counter()

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

谢谢提供的信息。我已经安装了chatterbot并获得了错误信息。我将尝试降级到Python 3.7。 - dudung

21

使用这个丑陋的猴子补丁进行死贴:

import time
time.clock = time.time

作为最后的手段可行,但不推荐使用。


非常适用于AWS Lambda。谢谢! - ka8725
哇,好的,知道它很有用。谢谢你让我们知道! - fviktor
比编辑cpython源代码的方法要好得多,因为当您重新部署/重新安装时,该方法将再次失败。 - xjcl
最佳,即时修复 - Abhijeet Sinha
time.clock = time.process_time 这个也可以工作,对吧? - daparic
从Python 3.3开始,time.clock = time.perf_counter具有更高的精度。 - undefined

19

原因:time.clock已被弃用。

解决方案:

第一步

  • 打开你的C文件夹,并在搜索栏中搜索 site-packages。你将看到如下结果:

site-packages

  • 右键单击标记为红色的结果,并选择打开文件位置

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

第二步

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

步骤2顺利进行,轻松快捷,谢谢 :) - 4nkitpatel
1
最简单的解决方案。 - Mahmudul Hasan

5

前往代码路径 C:\Users\Mr\anaconda3\envs\pythonProject2\Lib\site-packages\sqlalchemy\util,选择 compat.py 文件并查找代码中的 time.clock

然后将 time.clock 替换为 time.time 并保存文件。


2
这是否适用于 OP 的情况?像这样修改库的代码真的是个好主意吗? - AMC
虽然这可能不是正确和适当的答案,但那个答案对我有用,因为我对Python有一点经验。 - Flowra

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

如所说,它已被弃用,这意味着只需使用具有该模块的最新版本库。例如,根据您所依赖的情况,删除和安装

Crypto==1.4.1,或Mako==1.1.2,或SQLAlchemy==1.3.6 //等等

重要的是,您不必降级Python版本,因为这会在后面追上您。只需更新软件包以更晚的版本,与Python 3.8 兼容即可。


3
您使用的模块用于生成密钥的方法已经自Python 3.3版本开始被弃用,time.clock()。您可以降级为Python 3.7版本,或更改源代码以进行替换。您还应该为此打开一个问题。

3

time.clock 在许多旧库中使用。 现在 time.clock 已被删除,您必须单击错误中给出的路径。 这将导航到写有 time.clock 的行,并将其更改为 time.time


2

打开文件并前往错误信息所指示的行,将 time.clock() 更改为 time.perf_counter()。如果仍然无法运行,请将其修改为 time.time


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接