RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods

框架 django

开发语言 python

出现异常信息如下:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000028C91E74840>
Traceback (most recent call last):
  File "D:\myenv\bd\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "D:\myenv\bd\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run
    self.check(display_num_errors=True)
  File "D:\myenv\bd\lib\site-packages\django\core\management\base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\myenv\bd\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "D:\myenv\bd\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\myenv\bd\lib\site-packages\django\core\checks\model_checks.py", line 30, in check_all_models
    errors.extend(model.check(**kwargs))
  File "D:\myenv\bd\lib\site-packages\django\db\models\base.py", line 1284, in check
    errors.extend(cls._check_fields(**kwargs))
  File "D:\myenv\bd\lib\site-packages\django\db\models\base.py", line 1359, in _check_fields
    errors.extend(field.check(**kwargs))
  File "D:\myenv\bd\lib\site-packages\django\db\models\fields\__init__.py", line 913, in check
    errors = super(AutoField, self).check(**kwargs)
  File "D:\myenv\bd\lib\site-packages\django\db\models\fields\__init__.py", line 219, in check
    errors.extend(self._check_backend_specific_checks(**kwargs))
  File "D:\myenv\bd\lib\site-packages\django\db\models\fields\__init__.py", line 322, in _check_backend_specific_checks
    return connections[db].validation.check_field(self, **kwargs)
  File "D:\myenv\bd\lib\site-packages\django\db\backends\mysql\validation.py", line 49, in check_field
    field_type = field.db_type(self.connection)
  File "D:\myenv\bd\lib\site-packages\django\db\models\fields\__init__.py", line 644, in db_type
    return connection.data_types[self.get_internal_type()] % data
  File "D:\myenv\bd\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\myenv\bd\lib\site-packages\django\db\backends\mysql\base.py", line 174, in data_types
    if self.features.supports_microsecond_precision:
  File "D:\myenv\bd\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\myenv\bd\lib\site-packages\django\db\backends\mysql\features.py", line 53, in supports_microsecond_precision
    return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
  File "D:\myenv\bd\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\myenv\bd\lib\site-packages\django\db\backends\mysql\base.py", line 385, in mysql_version
    with self.temporary_connection() as cursor:
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "D:\myenv\bd\lib\site-packages\django\db\backends\base\base.py", line 591, in temporary_connection
    cursor = self.cursor()
  File "D:\myenv\bd\lib\site-packages\django\db\backends\base\base.py", line 254, in cursor
    return self._cursor()
  File "D:\myenv\bd\lib\site-packages\django\db\backends\base\base.py", line 229, in _cursor
    self.ensure_connection()
  File "D:\myenv\bd\lib\site-packages\django\db\backends\base\base.py", line 213, in ensure_connection
    self.connect()
  File "D:\myenv\bd\lib\site-packages\django\db\backends\base\base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "D:\myenv\bd\lib\site-packages\django\db\backends\mysql\base.py", line 274, in get_new_connection
    conn = Database.connect(**conn_params)
  File "D:\myenv\bd\lib\site-packages\pymysql\connections.py", line 353, in __init__
    self.connect()
  File "D:\myenv\bd\lib\site-packages\pymysql\connections.py", line 633, in connect
    self._request_authentication()
  File "D:\myenv\bd\lib\site-packages\pymysql\connections.py", line 932, in _request_authentication
    auth_packet = _auth.caching_sha2_password_auth(self, auth_packet)
  File "D:\myenv\bd\lib\site-packages\pymysql\_auth.py", line 265, in caching_sha2_password_auth
    data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
  File "D:\myenv\bd\lib\site-packages\pymysql\_auth.py", line 144, in sha2_rsa_encrypt
    "'cryptography' package is required for sha256_password or caching_sha2_password auth methods"
RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods

是因为缺少cryptography包,

可以使用 pip install cryptography 安装cryptography包,

如果安装cryptography失败,先检查一下自己的pip包是否为最新版本,在settings>Project>Project Interpreter里查看pip的版本

接下来使用easy_install -U pip 更新pip

然后再用 pip install cryptography 安装cryptography包

解决方案如下:

(bd) D:\pyproject\BD>pip install cryptography
Collecting cryptography
  Using cached cryptography-35.0.0-cp36-abi3-win_amd64.whl (2.1 MB)
Collecting cffi>=1.12
  Using cached cffi-1.15.0-cp36-cp36m-win_amd64.whl (178 kB)
Collecting pycparser
  Using cached pycparser-2.20-py2.py3-none-any.whl (112 kB)
Installing collected packages: pycparser, cffi, cryptography
Successfully installed cffi-1.15.0 cryptography-35.0.0 pycparser-2.20
WARNING: You are using pip version 21.2.4; however, version 21.3 is available.
You should consider upgrading via the 'D:\myenv\bd\Scripts\python.exe -m pip install --upgrade pip' command.

python成功启动django程序提示信息如下:

D:\myenv\bd\Scripts\python.exe D:/pyproject/BD/bd_project/manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 22, 2021 - 09:12:53
Django version 1.11.11, using settings 'bd_project.settings.dev'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.