AttributeError:Python 3.8 中的模块“time”没有属性“clock”
- 2025-03-13 09:05:00
- admin 原创
- 79
问题描述:
我编写了生成公钥和私钥的代码。它在 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'
解决方案 1:
来自Python 3.8 文档:
此函数
time.clock()
自 Python 3.3 起被弃用,现已被移除:请根据您的需求使用time.perf_counter()
或time.process_time()
来获得明确定义的行为。(由 Matthias Bussonnier 在bpo-36895中贡献。)
解决方案 2:
检查您是否正在使用 PyCrypto,如果是,请卸载它并安装PyCryptodome,它是 PyCrypto 的一个分支。
项目问题页面上提到 PyCrypto 已死亡
由于这两个库可以共存,因此这也可能是一个问题
pip3 uninstall PyCrypto
pip3 install -U PyCryptodome
解决方案 3:
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: 0.0382 ---------------------------> 0.0384
# Windows: 26.1224 ---------------------------> 36.1566
那么应该选择哪个功能呢?
处理器时间:这是特定进程在 CPU 上执行的时间。睡眠、等待 Web 请求或仅执行其他进程的时间不计入此时间。
+ 使用`time.process_time()`
挂钟时间:这指的是“挂在墙上的钟上”已经过了多少时间,即超出实际时间。
+ 使用`time.perf_counter()`
- `time.time()`还可以测量挂钟时间,但可以重置,这样你就可以回到过去
- `time.monotonic()`无法重置(单调 = 只能向前移动),但精度低于`time.perf_counter()`
解决方案 4:
在坟墓中贴出这个丑陋的猴子补丁:
import time
time.clock = time.time
作为最后的手段,但不推荐。
解决方案 5:
原因: time.clock
已弃用
解决方案:
步骤 1
只需转到C 文件夹并在搜索栏中搜索site-packages。您将看到如下结果:
右键单击标记为红色的结果,然后打开文件位置。
然后查找sqlalchemy/util/compat.py文件并打开它。然后使用 ctrl+f搜索time.clock并将其替换为time.time
第 2 步
转到错误的最后一行并转到该目录并打开该特定文件。
使用 ctrl+f搜索time.clock并将其替换为time.time
解决方案 6:
转到代码 C:\Users\Mr\anaconda3\envs\pythonProject2\Lib\site-packages\sqlalchemy/util 并选择 compat.py 并time.clock
在代码中搜索。
然后替换time.clock
并time.time
保存。
解决方案 7:
AttributeError: module 'time' has no attribute 'clock'
如上所述,它已被弃用,这意味着只需使用具有该模块的最新版本的库。例如,根据您拥有的依赖项,删除并安装
Crypto==1.4.1,或 Mako==1.1.2 或 SQLAlchemy==1.3.6 //等
这个想法是,你不必降级你的 Python 版本,因为这会在以后赶上你。只需将软件包更新为与 Python 3.8 兼容的较新版本即可
解决方案 8:
用于生成密钥的模块调用自 python 3.3 time.clock()以来已弃用的方法。
您可以降级到 Python 3.7 或更改源代码来替换它。您也应该为此打开一个问题。
解决方案 9:
time.clock
在许多旧库中都有使用。现在它time.clock
已被删除,必须单击错误中给出的路径。这将导航到写入的行time.clock
,然后将其更改为time.time
。
解决方案 10:
打开文件并转到错误消息所指出的行,将该time.clock()
行更改为time.perf_counter()
,如果仍然不起作用,请将其修改为time.time
解决方案 11:
我在我的项目 neehack.com 中使用 AES 加密字符串时遇到了同样的问题,我通过更新venv/lib/python3.8/site-packages/Crypto/Random/_UserFriendlyRNG.py
第 77 行修复了这个问题t = time.time()
,现在问题已经修复。
解决方案 12:
就我而言,我将time.clock()替换为time.time()
解决方案 13:
如果涉及数据库,请升级它。
pip install --upgrade flask_sqlalchemy
解决方案 14:
只需打开文件“/home/paulsteven/.local/lib/python3.8/site-packages/Crypto/Random/_UserFriendlyRNG.py”,第 77 行
并改变
t = time.clock()
到
t = time.time
解决方案 15:
打开调试错误行的文件。并将 t = time.clock() 更改为 t = time.time
解决方案 16:
在 python3.6 环境上测试:
>>> print("%s %s" % (time.process_time(), time.clock()))
>>> 0.288050639 0.288056
这意味着 process_time 是 lib 所需要的。
解决方案 17:
用 time.time() 替换 time.clock() 对我有用。
time.process_time() 对我来说不起作用,并产生了其他问题,如浮点错误等。
扫码咨询,免费领取项目管理大礼包!