import flask could not be resolved from source pylance

Question:

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!'

enter image description here

enter image description here

Asked By: Jag99

||

Answers:

When I did not install the module "flask", I ran into the problem you described:

enter image description here

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":

enter image description here

If it still shows that the module could not be resolved, it is recommended that you reinstall the module "flask".

Answered By: Jill Cheng

I experienced the same situation until I changed the virtual environment of my VS Code to indicate the correct value that I should use:

screenshot of my VSCode

Answered By: deji techserv

In VS Code, Go to "Python: Select interpreter" by Ctrl + Shift + P.
Choose python interpreter (‘base’: conda)

Answered By: Bibin
  1. Firstly Create a Virtual Environment on your terminal
  2. then install your flask by pip install flask
  3. after install CTRL+SHIFT+P
  4. Search Python Interpreter
  5. Select Your virtual Environment

Problem Will bi fixed. I have also faced same problem. but I have fixed it following this procedure

Answered By: Saroar Zahan Sojib

In case you are using a virtual environment;

  1. Create a virtual environment.

    python3.9 -m venv --without-pip virtual

  2. Activate the virtual environment.

    source virtual/bin/activate

  3. Install the pip for the created virtual environment.

    curl https://bootstrap.pypa.io/get-pip.py | python

  4. Install flask into the virtual env.

    pip install flask

  5. Create the python file. For your case,

    touch server.py

  6. Open file and import the module

  7. If it underlines again, install pip again while the .py file is still open.

    pip install flask

Answered By: Levi L.

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.

Answered By: Bhumika Sethi

go to your vs code terminal then type this command

  1. sudo apt install python3-venv

  2. python3 -m venv my-project-env

  3. source my-project-env/bin/activate

  4. pip install flask

  5. after install CTRL+SHIFT+P and search for Python Interpreter
    enter image description here

  6. Select Your virtual Environment my-project-env which has created in above
    enter image description here

  7. now check the output. problem will be solved like this.
    enter image description here

Answered By: Saroar Zahan Sojib