bcrypt icon indicating copy to clipboard operation
bcrypt copied to clipboard

AttributeError: module 'bcrypt' has no attribute '__about__' with new 4.1.1 version

Open roland-robert opened this issue 1 year ago • 24 comments

For bcrypt 4.1.1, I get this error/warning when I run this code (code which is in FastAPI documentation) I have passlib 1.7.4

from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
pwd_context.hash('test')   

output

(trapped) error reading bcrypt version
Traceback (most recent call last):
  File "C:\path\venv\Lib\site-packages\passlib\handlers\bcrypt.py", line 620, in _load_backend_mixin
    version = _bcrypt.__about__.__version__
              ^^^^^^^^^^^^^^^^^
AttributeError: module 'bcrypt' has no attribute '__about__'
'$2b$12$dcl0YFoHz6.pL/dtOwbfO.r3CRI416BLq6vJEf0EmT4CHsqqHm7FC'

It's not breaking anything, because code still runs.

This is not the case with bcrypt 4.0.1

roland-robert avatar Nov 29 '23 16:11 roland-robert

This is an issue with how passlib attempts to read a version (for logging only) and fails because it's loading modules that no longer exist in bcrypt 4.1.x. I'd suggest filing an issue against them for this.

https://foss.heptapod.net/python-libs/passlib/-/blob/branch/stable/passlib/handlers/bcrypt.py#L619-623

reaperhulk avatar Nov 29 '23 16:11 reaperhulk

Yep, passlib seems to be abandoned and the new bcrypt doesn't work with it. Needs to force bcrypt==4.0.1 to keep using passlib.

hyperknot avatar Dec 01 '23 22:12 hyperknot

As the OP indicates here, passlib will work with latest bcrypt, it simply emits a warning. You should be able to silence that warning with a logging configuration.

alex avatar Dec 01 '23 22:12 alex

Props to the opener of the issue on passlib: https://foss.heptapod.net/python-libs/passlib/-/issues/190

Edited to add: silencing the warning via logging config worked fine for me, hat-tip to @alex

logging.getLogger('passlib').setLevel(logging.ERROR)

patrick-m-m avatar Dec 15 '23 19:12 patrick-m-m

Like others have pointed, passlib is not actively updated and it has dependencies that have changed behaviour over time. I recently had this error (below) that indirectly triggered a CORS error on my frontend, due to middleware misinterpretation.

> (trapped) error reading bcrypt version
> Traceback (most recent call last):
>   File "C:\path\venv\Lib\site-packages\passlib\handlers\bcrypt.py", line 620, in _load_backend_mixin
>     version = _bcrypt.__about__.__version__
>               ^^^^^^^^^^^^^^^^^

I resolved it by removing the passlib module and simply using the bcrypt directly for hashing and verification :

import bcrypt

# Hash a password using bcrypt
def hash_password(password):
    pwd_bytes = password.encode('utf-8')
    salt = bcrypt.gensalt()
    hashed_password = bcrypt.hashpw(password=pwd_bytes, salt=salt)
    return hashed_password

# Check if the provided password matches the stored password (hashed)
def verify_password(plain_password, hashed_password):
    password_byte_enc = plain_password.encode('utf-8')
    return bcrypt.checkpw(password = password_byte_enc , hashed_password = hashed_password)

Reference: #https://www.geeksforgeeks.org/hashing-passwords-in-python-with-bcrypt/

creativebash avatar Jan 21 '24 10:01 creativebash

For bcrypt 4.1.1, I get this error/warning when I run this code (code which is in FastAPI documentation) I have passlib 1.7.4

from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
pwd_context.hash('test')   

output

(trapped) error reading bcrypt version
Traceback (most recent call last):
  File "C:\path\venv\Lib\site-packages\passlib\handlers\bcrypt.py", line 620, in _load_backend_mixin
    version = _bcrypt.__about__.__version__
              ^^^^^^^^^^^^^^^^^
AttributeError: module 'bcrypt' has no attribute '__about__'
'$2b$12$dcl0YFoHz6.pL/dtOwbfO.r3CRI416BLq6vJEf0EmT4CHsqqHm7FC'

It's not breaking anything, because code still runs.

This is not the case with bcrypt 4.0.1

just downgrade your bcrypt. i did it to 3.2.2. it works. it has __about attribute. hope my comment helps.

ArjunJayakrishnan avatar Jan 28 '24 12:01 ArjunJayakrishnan

just downgrade your bcrypt. i did it to 3.2.2. it works. it has __about attribute. hope my comment helps.

And go without updates for a security-relevant library? :grimacing:

bkis avatar Jan 29 '24 19:01 bkis

I resolved it by removing the passlib module and simply using the bcrypt directly for hashing and verification

I like this. Not a fan of using inactive libraries especially for straightforward things like this.

Nikhil22 avatar Mar 04 '24 11:03 Nikhil22