Unable to import 'request' module in Flask - "type object 'Flask' has no attribute 'request'"

after installing the requests module into my virtual environment, my flask application can no longer import/access the request module. i am able to import requests successfully, but not request: enter image description here As we can see, 'request' is greyed out and unable to be accessed as it previously was in my application. When I hover over the greyed 'request' it says '"request" is not accessed, Pylance' This is the output of pip list in my virtual environment enter image description here

asked Nov 26, 2021 at 10:16 Ivan Lendl Ivan Lendl 125 12 12 bronze badges "request" is not accessed, means that is never used in you code. Commented Nov 26, 2021 at 10:19

1 Answer 1

import webbrowser from requests import request from flask import request url = 'https://some_page.html' webbrowser.open_new_tab(url) request. 

On these moment you can use request.

Without any line with command request, you have information with "the greyed 'request' it says '"request" is not accessed, Pylance."

answered Nov 26, 2021 at 10:49 9 3 3 bronze badges

why would you import request from requests and immediately override it with request from flask ? In the OP case PyLance just informs them that they not use flask.request in their code.

Commented Nov 26, 2021 at 10:54

Also, both requests.request and flask.request are never used in your code, so they will be grayed out too as unused imports - the same like in OP case.

Commented Nov 26, 2021 at 11:00

On this case you can use only request command, when I use import I must use all times requests.request. Maybe I'm wrong, but sometimes is easier for me

Commented Nov 26, 2021 at 11:05

You don't understand, do you? Both imports are never used. So your code will work without them, why do you import them? Ignore the fact that your example is irrelevant to OP question. And because you first import request from requests package and immediately, on the next line you import request from flask if it happens to use name request in your code (which you don't) name request will be the one from flask package.

Commented Nov 26, 2021 at 11:10

As others pointed out, these are unused import statements. Thats problem 1, but more importantly, if you actually want to use "from requests import request", you couldnt, because you're importing "from flask import request" under the same name. To avoid this, you can "from flask import request as flask_request" and use this under the name "flask_request". Now you can use both flask request and python request at the same time and avoid confusion of the two.