属性错误:模块“时间”在 Python 3.8 中没有属性“时钟”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58569361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: module 'time' has no attribute 'clock' in Python 3.8
提问by Angus Tay
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.
我已经编写了代码来生成公钥和私钥。它在 Python 3.7 上运行良好,但在 Python 3.8 中失败。我不知道它在最新版本中是如何失败的。帮我解决一些问题。
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:
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'
回答by Angus Tay
From the Python 3.8 doc:
The function
time.clock()
has been removed, after having been deprecated since Python 3.3: usetime.perf_counter()
ortime.process_time()
instead, depending on your requirements, to have well-defined behavior. (Contributed by Matthias Bussonnier in bpo-36895.)
time.clock()
自 Python 3.3 起已被弃用后,该函数已被删除:使用time.perf_counter()
或time.process_time()
代替,根据您的要求,具有明确定义的行为。(由 Matthias Bussonnier 在bpo-36895 中提供。)
回答by Florian Bernard
The module you use to generate key call a method that have been depreciated since python 3.3 time.clock().
用于生成密钥的模块调用自 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.
您可以降级到 python 3.7 或更改源代码以替换它。你也应该为此打开一个问题。
回答by joash
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
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
这个想法是你不必降级你的python版本,因为这会在以后赶上你。只需将软件包更新为与 Python 3.8 兼容的较晚的软件包
回答by xjcl
time.clock()
was removedbecause it had platform-dependent behavior:
time.clock()
被删除,因为它具有平台相关的行为:
- On Unix, return the current processor time as a floating point number expressed in seconds.
On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number
print(time.clock()); time.sleep(10); print(time.clock()) # Linux : 0.0382 0.0384 # see Processor Time # Windows: 26.1224 36.1566 # see Wall-Clock Time
- 在Unix 上,将当前处理器时间返回为以秒表示的浮点数。
在Windows 上,此函数返回自第一次调用此函数以来经过的挂钟秒数,作为浮点数
print(time.clock()); time.sleep(10); print(time.clock()) # Linux : 0.0382 0.0384 # see Processor Time # Windows: 26.1224 36.1566 # see Wall-Clock Time
So which function to pick instead?
那么该选择哪个函数呢?
Processor Time: This is how long this specific program spends actively being executed on the CPU. Sleep or waiting for a web request will not contribute to this.
time.process_time()
: Does not include sleep and is process-wide.
Wall-Clock Time: This refers to how much time has passed "on a clock hanging on the wall", i.e. outside real time.
time.perf_counter()
: Includes sleep and is system-wide.- This is similar to
time.monotonic()
but promises higher precision.
- This is similar to
处理器时间:这是此特定程序在 CPU 上主动执行所花费的时间。睡眠或等待网络请求不会对此做出贡献。
time.process_time()
:不包括睡眠并且是进程范围的。
挂钟时间:这是指“挂在墙上的时钟”已经过去了多少时间,即在实时之外。
time.perf_counter()
:包括睡眠并且是系统范围的。- 这类似于
time.monotonic()
但承诺更高的精度。
- 这类似于