ImportError:无法从“flask”导入名称“request”

Posted

技术标签:

【中文标题】ImportError:无法从“flask”导入名称“request”【英文标题】:ImportError: cannot import name 'request' from 'flask' 【发布时间】:2022-01-12 10:34:46 【问题描述】:

通过 Pycharm 运行 py 文件时出现以下错误:

ImportError: cannot import name 'request' from 'flask'

Traceback (most recent call last):
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/resources/token.py", line 2, in <module>
    from flask import request
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/__init__.py", line 17, in <module>
    from werkzeug.exceptions import abort
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/werkzeug/__init__.py", line 1, in <module>
    from .serving import run_simple as run_simple
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/werkzeug/serving.py", line 16, in <module>
    import platform
  File "/usr/lib/python3.7/platform.py", line 116, in <module>
    import sys, os, re, subprocess
  File "/usr/lib/python3.7/subprocess.py", line 155, in <module>
    import threading
  File "/usr/lib/python3.7/threading.py", line 8, in <module>
    from traceback import format_exc as _format_exc
  File "/usr/lib/python3.7/traceback.py", line 5, in <module>
    import linecache
  File "/usr/lib/python3.7/linecache.py", line 11, in <module>
    import tokenize
  File "/usr/lib/python3.7/tokenize.py", line 35, in <module>
    from token import *
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/resources/token.py", line 2, in <module>
    from flask import request
ImportError: cannot import name 'request' from 'flask' (/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/__init__.py)

Process finished with exit code 1

代码如下:

from http import HTTPStatus
from flask import request
from flask_restful import Resource
from flask_jwt_extended import (
    create_access_token,
    jwt_refresh_token_required,
    create_refresh_token,
    get_jwt_identity,
    jwt_required,
    get_raw_jwt
)

from utils import check_password
from models.user import User

black_list = set()


class TokenResource(Resource):

    def post(self):

        json_data = request.get_json()

        email = json_data.get('email')
        password = json_data.get('password')

        user = User.get_by_email(email=email)

        if not user or not check_password(password, user.password):
            return 'message': 'username or password is incorrect', HTTPStatus.UNAUTHORIZED

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(identity=user.id)

        return 'access_token': access_token, 'refresh_token': refresh_token, HTTPStatus.OK


class RefreshResource(Resource):

    @jwt_refresh_token_required
    def post(self):
        current_user = get_jwt_identity()

        token = create_access_token(identity=current_user, fresh=False)

        return 'token': token, HTTPStatus.OK


class RevokeResource(Resource):

    @jwt_required
    def post(self):
        jti = get_raw_jwt()['jti']

        black_list.add(jti)

        return 'message': 'Successfully logged out', HTTPStatus.OK

据我所知,Flask 请求对象包含客户端(例如浏览器)发送到应用程序的数据(URL 参数、POST 数据等) 如此处所述:is "from flask import request" identical to "import requests"?

【问题讨论】:

【参考方案1】:

你有一个循环导入。这是因为您的token 模块隐藏了标准库模块。从追溯:

  File "/usr/lib/python3.7/tokenize.py", line 35, in <module>
    from token import *
  File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/resources/token.py", line 2, in <module>
    from flask import request

模块tokenize.py 正在寻找token.py 并获取您的文件而不是标准库文件。它注意到请求的导入是正在处理的导入并且失败。

重命名你的文件token.py

【讨论】:

其实可以的...很棒

以上是关于ImportError:无法从“flask”导入名称“request”的主要内容,如果未能解决你的问题,请参考以下文章

flask importerror:无法导入名称存储

使用flask_sqlalchemy“ImportError:无法导入名称”

Flask,Pymongo和Mongoengine - ImportError:无法导入名称'app'

我无法从另一个文件夹导入函数,它导致导入错误(ImportError:无法导入名称'token_required')

Flask开发 导入flask_uploads包时提示“IMPORTERROR: CANNOT IMPORT NAME ‘SECURE_FILENAME‘ FROM ‘WERKZEUG‘“

Flask开发 导入flask_uploads包时提示“IMPORTERROR: CANNOT IMPORT NAME ‘SECURE_FILENAME‘ FROM ‘WERKZEUG‘“