我正在尝试在Ubuntu 16.04上的Python 3.5中使用库secrets。它没有随着python安装而来,且我无法通过pip进行安装。是否有方法使其在Python 3.5上正常工作?
我正在尝试在Ubuntu 16.04上的Python 3.5中使用库secrets。它没有随着python安装而来,且我无法通过pip进行安装。是否有方法使其在Python 3.5上正常工作?
很烦人的是,这里没有适用于PyPi模块的内容,且Ubuntu使用古老版本的Python,希望有人能修复这个问题。与此同时:
如果您使用旧版本的Python(>=2.4并且<=3.5),您可以使用os库中的urandom
函数生成密钥。
例如:
from os import urandom
urandom(16) # same as token_bytes(16)
urandom(16).hex() # same as token_hex(16) (python >=3.5)
为了使某个东西向后兼容,同时在支持时仍使用新的 secrets 库,你可以这样做:try:
from secrets import token_hex
except ImportError:
from os import urandom
def token_hex(nbytes=None):
return urandom(nbytes).hex()
pip install --user python2-secrets
您正在尝试使用的模块在Python 3.5版本之前并不是Python的一部分。
看起来在那个版本中,也无法从pip下载secrets模块。
$ pip install secrets
Collecting secrets
Could not find a version that satisfies the requirement secrets (from versions: ) No matching distribution found for secrets
当您在 Python 3.6 环境下工作时,该模块可以直接导入,因为它是标准库的一部分:
Python 3.6.3 (default, Mar 7 2018, 21:08:21) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information.
>>> import secrets
>>> print(secrets)
<module 'secrets' from '/home/mikel/.pyenv/versions/3.6.3/lib/python3.6/secrets.py'>
pip install secret
代替。