【问题标题】:ImportError: Could not import 'rest_framework_simplejwt.authentication.JWTAuthentication'ImportError:无法导入“rest_framework_simplejwt.authentication.JWTAuthentication”
【发布时间】:2019-09-10 05:04:03
【问题描述】:

我正在尝试在 GCP 上部署一个 django 应用程序,但是当我尝试进行迁移时,它给了我这个错误:

ImportError: 无法导入 API 的“rest_framework_simplejwt.authentication.JWTAuthentication” 设置'DEFAULT_AUTHENTICATION_CLASSES'。 ModuleNotFoundError: 否 名为“rest_framework_simplejwt”的模块。

Settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated'
    ],
'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ]
}

SIMPLE_JWT = {
    'ALGORITHM': 'HS256',
    'SIGNING_KEY': SECRET_KEY,
    'VERIFYING_KEY': None,
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=800),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=2),
}
OAUTH2_PROVIDER = {
        'ACCESS_TOKEN_EXPIRE_SECONDS': 60 * 15,
        'OAUTH_SINGLE_ACCESS_TOKEN': True,
        'OAUTH_DELETE_EXPIRED': True
 }

requirements.txt

django-cors-headers
pyjwt
djangorestframework
djangorestframework-jwt==1.11.0

我错过了什么?

更新 我安装了rest_framework_simplejwt,现在错误转移到:

没有名为“rest_framework_simplejwt.tokens”的模块

【问题讨论】:

  • 您在 GCP 上进行什么样的部署?计算还是 App Engine?
  • App 引擎部署

标签: python django django-rest-framework


【解决方案1】:

您似乎混淆了两个包。 djangorestframework-jwt 你的 requirements.txt 是 no longer maintained。它提供了rest_framework_jwt.authentication.JSONWebTokenAuthentication 身份验证类。

但是,您实际使用的 rest_framework_simplejwt.authentication.JWTAuthentication 来自 pip 包 djangorestframework-simplejwt

因此您需要更新您的 requirements.txt。删除djangorestframework-jwt并添加djangorestframework-simplejwt

【讨论】:

  • 嘿,我试过你的方法,我仍然得到同样的错误。
【解决方案2】:

对我来说,djangorestframework-simplejwt4.4.0 升级到当前最新版本4.6.0 解决了这个问题。

pip3 install --upgrade djangorestframework-simplejwt

【讨论】:

    【解决方案3】:

    如果还有人遇到这个错误,这是djangorestframework-simplejwt的要求:

    Python (3.6, 3.7, 3.8)
    Django (2.0, 2.1, 2.2, 3.0)
    Django REST Framework (3.8, 3.9, 3.10)
    

    我将 Django 和 DRF 降级,问题已为我解决。

    pip uninstall django
    pip uninstall djangorestframework
    pip install --upgrade django==3.0
    pip install --upgrade djangorestframework==3.10
    pip install djangorestframework-simplejwt
    

    【讨论】:

      【解决方案4】:

      尝试在 Heroku 上部署时遇到同样的错误

      经过调查,我发现我的案例有 2 个问题:

      1] requirements.txt --> 这个文件没有更新,因此 'git add' 和 'git commit' 没有得到 djangorestframework-simplejwt 的新要求

      所以这里的解决方案是做一个新的 pip freeze > requirements.txt,git add/commit 并在 Heroku 上确认

      2] 其次,我发现 Heroku 没有找到我的 python3.8 在本地安装的 v4.6.0。相反,我不得不将 requirements.txt 编辑为 v4.4.0(pypi.org 上的当前版本),在解决此问题之前重做 git add/commit

      【讨论】:

        【解决方案5】:

        对我来说,通过重启虚拟环境解决问题

        【讨论】:

          【解决方案6】:

          我的问题还在于安装了 djangorestframework-jwt 和 djangorestframework_simplejwt。卸载前者后,我的问题得到了解决。

          【讨论】:

            【解决方案7】:

            如果有人在使用 VS Code 并在 Docker 容器中运行他们的 Django 应用程序时仍然遇到这个问题,我通过将我的 Python 版本降级到 3.9 而不是 3.10 来解决这个问题,然后重建我的容器。

            在您的Dockerfile 中,确保FROM python:3.9 与文档页面上当前支持的python 版本相匹配:https://django-rest-framework-simplejwt.readthedocs.io/en/latest/index.html,在本例中为版本3.9。我的之前设置为3.10,在撰写本文时,它不受支持。然后重新构建你的 docker 镜像。

            docker-compose down

            docker-compose up -d --build

            另外,如果您使用 pipenv 来管理您的包,请在您的 Pipfilepython_version = "3.9" 中更改您的 python 版本。然后运行pipenv install 安装更改。

            【讨论】:

              猜你喜欢
              • 2018-02-22
              • 2022-01-15
              • 2019-11-05
              • 1970-01-01
              • 1970-01-01
              • 2018-06-26
              • 2012-10-10
              相关资源
              最近更新 更多