passlib 使用 bcrypt 哈希算法时:AttributeError: module ‘bcrypt‘ has no attribute ‘__about__‘_(trapped) error reading bcrypt version traceback (
先上终端虚拟环境下验证所用源码:
$ python3
Python 3.8.10 (default, Mar 11 2025, 17:45:31)
[GCC 9.4.0] on linux
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>>> from passlib.context import CryptContext
>>> pwd_context = CryptContext(schemes=[\'bcrypt\'],deprecated=\'auto\')
>>> hash_pass = pwd_context.hash(\'secret\')
(trapped) error reading bcrypt version
Traceback (most recent call last):
File \"/data/Project/python/github_fastapi_project/fast_api_test_code/fast_api_test_code_env/lib/python3.8/site-packages/passlib/handlers/bcrypt.py\", line 620, in _load_backend_mixin
version = _bcrypt.__about__.__version__
AttributeError: module \'bcrypt\' has no attribute \'__about__\'。
打印具体的属性信息(4.3.0版本):
>>> import bcrypt
>>> print(dir(bcrypt))
[\'__all__\', \'__author__\', \'__builtins__\', \'__cached__\', \'__copyright__\', \'__doc__\', \'__email__\', \'__file__\', \'__license__\', \'__loader__\', \'__name__\', \'__package__\', \'__path__\', \'__spec__\', \'__summary__\', \'__title__\', \'__uri__\', \'__version__\', \'_bcrypt\', \'checkpw\', \'gensalt\', \'hashpw\', \'kdf\']没有\'__about__。
接着尝试更新到最新版本解决:
pip install --upgrade bcrypt
Requirement already up-to-date: bcrypt in ./fast_api_test_code_env/lib/python3.8/site-packages (4.3.0)
(fast_api_test_code_env)
已是最新的4.3.0版本。
之后去社区查找类似问题,根据提供的方案将版本降低到:
卸载最新版本:
pip uninstall bcrypt
Found existing installation: bcrypt 4.3.0
Uninstalling bcrypt-4.3.0:
Would remove:
/data/Project/python/github_fastapi_project/fast_api_test_code/fast_api_test_code_env/lib/python3.8/site-packages/bcrypt-4.3.0.dist-info/*
/data/Project/python/github_fastapi_project/fast_api_test_code/fast_api_test_code_env/lib/python3.8/site-packages/bcrypt/*
Proceed (y/n)? y
Successfully uninstalled bcrypt-4.3.0
(fast_api_test_code_env)
安装指定版本:
$ pip install bcrypt==4.0.1
Collecting bcrypt==4.0.1
Downloading bcrypt-4.0.1-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (593 kB)
|████████████████████████████████| 593 kB 1.0 MB/s
Installing collected packages: bcrypt
Successfully installed bcrypt-4.0.1
(fast_api_test_code_env)
接着查看安装的版本:
$ python3
Python 3.8.10 (default, Mar 11 2025, 17:45:31)
[GCC 9.4.0] on linux
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>>> import bcrypt
>>> print(dir(bcrypt))
[\'__about__\', \'__all__\', \'__author__\', \'__builtins__\', \'__cached__\', \'__copyright__\', \'__doc__\', \'__email__\', \'__file__\', \'__license__\', \'__loader__\', \'__name__\', \'__package__\', \'__path__\', \'__spec__\', \'__summary__\', \'__title__\', \'__uri__\', \'__version__\', \'_bcrypt\', \'absolute_import\', \'checkpw\', \'division\', \'gensalt\', \'hashpw\', \'hmac\', \'kdf\', \'os\', \'warnings\']
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>>