I am trying to use the library secrets on Python 3.5 on Ubuntu 16.04. It does not come with the python installation and I am not able to install it through pip. Is there a way to get it to work on python 3.5?
In Python 3.x use pip install secret
instead
You can use the backport of the secrets module for Python 2.7, 3.4 and 3.5 by the name of python2-secrets. (the name is a bit confusing in my opinion)
Installation:
pip install --user python2-secrets
The fact that there is no PyPi module for this and Ubuntu uses ancient python versions is pretty annoying, it would be nice if someone could fix this. In the meantime:
To generate secrets in older versions of Python (>= 2.4 and <= 3.5) you can use the urandom
function from the os library.
Example:
from os import urandom
urandom(16) # same as token_bytes(16)
urandom(16).hex() # same as token_hex(16) (python >=3.5)
To make something backwards compatible that still uses the new secrets library when supported you could do something like
try:
from secrets import token_hex
except ImportError:
from os import urandom
def token_hex(nbytes=None):
return urandom(nbytes).hex()
The module you are trying to use wasn't part of Python as of version 3.5.
It looks like in that version secrets can't be downloaded from pip either
$ pip install secrets
Collecting secrets
Could not find a version that satisfies the requirement secrets (from versions: ) No matching distribution found for secrets
When working under a Python 3.6 environment that module can be imported right away, as it's part of the standard library:
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'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With