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
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.clock为time.time并保存。
右键单击标记为红色的此结果,然后打开文件位置
然后查找sqlalchemy/util/compat.py文件并打开它。并使用 ctrl+f搜索time.clock并将其替换为time.time
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           2194 次  |  
        
|   最近记录:  |