Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to install 'secrets' on python 3.5 (pip, ubuntu 3.5)

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?

like image 243
Harshwardhan Jain Avatar asked Mar 11 '18 09:03

Harshwardhan Jain


4 Answers

In Python 3.x use pip install secret instead

like image 178
Mohamed Abdillah Avatar answered Nov 03 '22 16:11

Mohamed Abdillah


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
like image 31
Vicente Avatar answered Nov 03 '22 16:11

Vicente


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()
like image 22
Bitbored Avatar answered Nov 03 '22 16:11

Bitbored


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'>
like image 2
WorkShoft Avatar answered Nov 03 '22 18:11

WorkShoft