Reputation: 754
I'm learning Python and part of the course setting up a webserver using Flask. I followed the steps as per the Flask installation documentation and for some reason the flask module is underlined as shown below. When I hover my mouse, I get additional information as below.
import flask could not be resolved from source pylance
The server is running fine though. Should i be ignoring the notification? If not what dependency have i missed?
Below is the code to setup the server
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
Upvotes: 34
Views: 108663
Reputation: 556
The problem will be fixed. I have also faced the same issue, but I resolved it by following this procedure.
Upvotes: 42
Reputation: 117
This answer is for posterity
virtual env
is activated and the right interpreter selected (you can see this in bottom-right of VS code).virtual env
or not.
ex: pip show flask
in terminal should give the flask location in virtual env
not global.Ctrl+Shift+P
and type reload window
and click it. This should solve the issue as it did for me.Upvotes: 0
Reputation: 11
All these answers are very helpful but if you are still stuck then try this method in which after creating a Virtual Environment we will select our interpreter path manually:
First open your project in VSCode. Make Venv and then at right bottom of your editor see there is an option to select Python version like this:
Then click on python version and you can see window like this:
Then click on "Enter interpreter path" and Select Interpreter that you created recently which is located inside the scripts folder:
Hope this helps. Happy coding!
Upvotes: 0
Reputation: 260
go to your vs code terminal then type this command
sudo apt install python3-venv
python3 -m venv my-project-env
source my-project-env/bin/activate
pip install flask
after install CTRL+SHIFT+P and search for Python Interpreter
Select Your virtual Environment my-project-env which has created in above
Upvotes: 6
Reputation: 81
This happens when the Python interpreter on VS Code is not the same as that in your virtual environment. Click on the Python version on the lower left corner. In the "Select Interpreter" bar, select the venv Python or create a new interpreter path by copying the same from your Python file in the venv/bin directory.
Upvotes: 7
Reputation: 11
In case you are using a virtual environment;
Create a virtual environment.
python3.9 -m venv --without-pip virtual
Activate the virtual environment.
source virtual/bin/activate
Install the pip for the created virtual environment.
curl https://bootstrap.pypa.io/get-pip.py | python
Install flask into the virtual env.
pip install flask
Create the python file. For your case,
touch server.py
Open file and import the module
If it underlines again, install pip again while the .py file is still open.
pip install flask
Upvotes: 1
Reputation: 409
In VS Code, Go to "Python: Select interpreter" by Ctrl + Shift + P. Choose python interpreter ('base': conda)
Upvotes: 8
Reputation: 1
I experienced the same situation until I changed the virtual environment of my VS Code to indicate the correct value that I should use:
Upvotes: 0
Reputation: 10364
When I did not install the module "flask
", I ran into the problem you described:
The reason is that the module "flask
" is not installed in the Python environment we currently use in VSCode.
Please use the shortcut key Ctrl+Shift+` to open a new VSCode terminal, it will automatically enter the currently selected environment, and then use the command "pip show flask" to check the installation location of the module "flask":
If it still shows that the module could not be resolved, it is recommended that you reinstall the module "flask".
Upvotes: 22