12

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

davidism
  • 110,080
  • 24
  • 357
  • 317
Jag99
  • 474
  • 2
  • 6
  • 18
  • 1
    it probably just means the python your editor is using is not the same interpreter that you're running when you run the server – M Z Jan 13 '21 at 02:25
  • Im using VS Code and installed Python 3.9.0 64 bit. What steps can i take to fix this please. – Jag99 Jan 13 '21 at 02:35
  • 6
    on the bottom left corner you should be able to see `Python 3.x.x`, click it, and make sure it is pointing to your system python – M Z Jan 13 '21 at 02:40
  • The system version is 2.7.16. When i select this interpreter, flask module still has the underline. – Jag99 Jan 13 '21 at 04:30
  • I came across the below link but not sure, what value i should assign for "python.autoComplete.extraPaths": ["./path-to-your-code"], in the vscode settings.json? https://stackoverflow.com/questions/65252074/import-path-to-own-script-could-not-be-resolved-pylance-reportmissingimports – Jag99 Jan 13 '21 at 04:58
  • 1
    What if you're using venv? Can you tell VSCode to use that specific Python module? – Peter Poliwoda Apr 19 '21 at 13:40
  • 1
    Hope this link helps https://techinscribed.com/python-virtual-environment-in-vscode/ – Jag99 Apr 21 '21 at 01:22

6 Answers6

11

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

Jill Cheng
  • 7,195
  • 1
  • 13
  • 22
  • 1
    Thanks Jill. Based on your solution, i noticed flask was installed in the project directory and not in the main python directory. I removed flask and reinstalled and its in the correct location as below: Location: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages – Jag99 Jan 13 '21 at 21:54
10
  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

4

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

Bibin
  • 109
  • 9
1

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

Levi L.
  • 11
  • 1
0

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

Adrian Mole
  • 43,040
  • 110
  • 45
  • 72
0

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.