35

When I am writing

from flask import Flask

One Yellow line is coming up under flask and stating Import "flask" could not be resolved from source Pylance (reportMissingModuleSource) . Also, I am able to do the work with this package also successfully. But the thing is, I am not able to use autosuggesstions for Classes and methods very well.

Further:

  1. I have checked that flask is installed successfully.
  2. Also I visited this thread https://github.com/microsoft/pylance-release/issues/236

And I set up my settings.json in vscode as follows:

"python.analysis.extraPaths": [
    "/media/sarimurrab/New Volume/COURSES/Flask/FlaskMigrateforDatabaseMigrations/2"
]

But Still, unable to resolve the error.

davidism
  • 121,510
  • 29
  • 395
  • 339
SARIM
  • 992
  • 3
  • 14
  • 22

12 Answers12

71

When I did not install the module "flask" in the Python environment currently used in VSCode:

enter image description here

Please use the command "pip --version" to check the source of the module installation tool "pip", the module is installed at this location:

enter image description here

Then, we can use the command "pip show flask" to check the installation location of the module "flask": (It checks whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode.)

enter image description here

If the "reportMissingModuleSource" message is still displayed here, please reload VS Code.

(F1, Developer: Reload Window)

enter image description here

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
  • 1
    It is installed in the correct directory. – SARIM Jan 20 '21 at 04:12
  • 2
    @Chaudhary Sarimurrab -Does the terminal still display "reportMissingModuleSource" after reloading VS Code? – Jill Cheng Jan 20 '21 at 04:22
  • Import "wtforms" could not be resolvedPylancereportMissingImports – SARIM Jan 20 '21 at 05:47
  • @Chaudhary Sarimurrab -Please install the module "wtforms" in the same Python environment.("_pip install wtforms_" or "_pip3 install wtforms_" ) – Jill Cheng Jan 20 '21 at 05:55
  • @Chaudhary Sarimurrab -Which terminal did you install the module on? (Is VS Code internal terminal?) Please follow the commands in my answer to check if the module "wtforms" is installed in the same location. – Jill Cheng Jan 20 '21 at 06:01
  • 2
    @Chaudhary Sarimurrab -Please check whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode. – Jill Cheng Jan 20 '21 at 06:07
  • Got It. Thanks for resolving my issue. – SARIM Jan 20 '21 at 06:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227642/discussion-between-chaudhary-sarimurrab-and-jill-cheng). – SARIM Jan 21 '21 at 14:43
  • Everything was correct for me with my virtual environment. Reloading was the only thing which got rid of the VS code error, thanks! Upvoting this now. `CMD/CTRl + shift + P` - search "reload window". – neo5_50 Jul 03 '21 at 09:49
  • My Flask module is in venv and after reloading the window, this issue is gone. Thanks. – B.D Oct 13 '22 at 15:34
57

Are you using a Virtualenv? If so make sure that VSCode is using the virtualenv as your python interpreter, otherwise it will not be able to pick up the packages that you installed inside this virtualenv.

To do so, click on the Python interpreter in your bottom bar, you should get a list of possible python interpreters including your virtualenv.

Marius Kimmina
  • 999
  • 6
  • 13
4

I had a similar issue while trying to import flask on vscode. I fixed it by using anaconda. Simply you install the flask module in your created environment example screenshot.

How to create a virtual env in anaconda:
1. On the left sidebar, click on environments.
2. Click create (at the bottom).
3. At the pop-up window, give your vir.env a name and select the language version.
4. Once created, you can start installing different modules in your environment.

I hope that helps!

Icaru5
  • 93
  • 1
  • 1
  • 7
4

For Linux Mint and for those who have installed flask, but VSCode doesn't find it:

  1. check the Flask path: pip show flask (should be smth like Location: /home/<username>/.local/lib/python3.8/site-packages
  2. in VSCode click left bottom button and choose the python interpreter, in my case I changed it from python3.9 to python3.8 as we can see it in the flask path.
Roy O'Bannon
  • 179
  • 1
  • 9
2

I have tried all of these and none of these steps worked for me. Finally, this worked:

Check your IDE's interpreter settings: If you are using an IDE like PyCharm or Visual Studio Code, make sure that you have selected the correct interpreter for your virtual environment in the IDE's settings.

Where do we check this in VSCode:

  1. Cmd + Shift + P
  2. Type "Python: Select Interpreter" in the search bar of the Command Palette and select the option from the list.
  3. If your virtual environment is already listed, select it from the list of interpreters. If not, select the "Enter Interpreter Path" option and manually locate the interpreter executable in your virtual environment.
  4. Interpreter for your virtual environment would be: <path_to_virtual_environment>/bin/python
1

That's because you have not chosen your path correctly, type:
pipenv --venv
then it will show you where your virtual env is installed. Check where the packages are installed in your env, and then type what comes to you from the shell\scripts or whatever\python, and the packages will work.

greybeard
  • 2,249
  • 8
  • 30
  • 66
1

I solved my problem by using a "Global" version of Python. Maybe Pylance had not updated to work with the version of Python I was using. interpreter language screenshot

1

I faced this issue because of using pip install flask instead of pip3 install flask.

ishaj
  • 91
  • 1
  • 4
0

A few answers (Jill's, Marius's, and Roy's) mention the fact that is necessary to choose the correct Python interpreter to make Pylance function properly. I would like to add the fact that this is still necessary to do when using a Jupyter Notebook with the correct Python kernel already chosen.

It is counterintuitive to choose both Python interpreter and notebook's Python kernel to make things work. It is even more counterintuitive considering the fact that Python interpreter's button (on the left bottom of the screen, on status bar) does not necessarily appear when a Jupyter Notebook is open, but when a Python script is open. For instance, in this screenshot, we see the little line under Scikit-learn's import, indicating a problem with the import (even though the import is successful). However, the correct Python kernel, with Scikit-learn installed, is already chosen. Only opening a Python script we notice that the Python interpreter is the reason of this behavior, because a wrong one is chosen, without Scikit-learn. In some sense, one could think that the reason behind this was a problem with the Python kernel or the Conda environment (it is common to experience this kind of problem when experimenting with Jupyter Notebook and Jupyter Lab). I hope this answer may help those who are searching for solving this problem in the specific context of Jupyter Notebooks inside VS Code. They could ignore the other answers because they could think it is not the case for them.

lucasresck
  • 129
  • 1
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 30 '21 at 19:40
  • Hi, @GinoMempin, I appreciate your comment. I have edited my question to include the citations to the other answers which already have mentioned the Python interpreter. I also developed more about the specific problem of Jupyter Notebooks inside VS Code, to make it more clear for those who are searching for solving this specific situation, given the fact this is not obvious at a first glance. – lucasresck Oct 31 '21 at 16:25
0

I was facing same issue. I tried all solutions from stack-overflow but none worked. But after lot of searching and time waste I found my silly mistake. I had created folder named 'flask' and stored my project there. I'm beginner and going through lot of such silly mistakes. Hope it would help, if somebody commits same mistake.

0

I think this is a Vscode problem ,just restart the Vscode .

Aghiad Alzein
  • 159
  • 1
  • 6
0

By the following steps, I solved this issue:

1)On the project directory create the .flaskenv file

  1. In the .flaskenv file write the following two lines:

FLASK_ENV=development

FLASK_APP=main.py

Please pay attention that main.py is my main file after writing flask run in the Vscode terminal, additionally, you have to create an env folder or requirement file proviosly.

Jamal Azizbeigi
  • 135
  • 1
  • 14