83

My Flask App server is running but I have three imports that cannot be resolved. unresolved imports

I have tried:

  1. reinstalling the imports individually
  2. reinstalling requirements.txt
  3. I configured VSCode Workspace with an extra path to my project folder (there is now a .vscode file within the root of my project folder where it was not before)
  4. I have updated my venv path settings in VSCode

Here is my file structure:

- > .vscode
- > client *(React front end)*
- > data
- > server *(Python/Flask back end)*
    - > app
    - > venv
    - config.py
    - README.md
    - requirements.txt *(this contains the 3 unresolved, along with several that are resolving)*
- .env
- .flaskenv
- .gitignore
- requirements.txt

Unfortunately none of these things have resolved my imports issue and my routes are still not working. Any ideas/suggestions?

Adam Smooch
  • 1,167
  • 1
  • 12
  • 27
SMKrizan
  • 1,049
  • 1
  • 6
  • 10

10 Answers10

137
  1. Open the Command Palette (Ctrl+Shift+P), then select the Python: Select Interpreter. From the list, select the virtual environment in your project folder that starts with .env.

  2. Run Terminal: Create New Integrated Terminal (Ctrl+Shift+` or from the Command Palette), which creates a terminal and automatically activates the virtual environment by running its activation script.

  3. Install sqlalchemy and mongoengine with command pip install. Once installing them successfully, there will intellisense when you import them and no warnings shown.

screenshot install package with pip within environment

Besides, the folder .vscode is to store Workspace settings as well as debugging and task configurations.

Leonard
  • 2,510
  • 18
  • 37
Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22
  • 2
    Re step 1: the list that appears when I click on 'Python: Select Interpreter' does not include one that starts with the name of my project's virtual environment. – SMKrizan Jul 23 '21 at 14:56
  • "Terminal: Create New Integrated Terminal" was not an option for me but "Terminal: Create New Terminal (In Active Workspace)" was and that worked for me. – influent Nov 12 '21 at 19:52
  • What if I already have the sqlalchemy and mongoengine installed? Pylance is still throwing me error, I've updated my "python.defaultInterpreterPath" appropriately to point to the virtual environment. – insanely_sin Oct 06 '22 at 20:09
80

To resolve the issue, perform the following steps:

  1. Open the Command Palette by pressing Ctrl+Shift+P on your keyboard.
  2. In the Command Palette, select Python: Clear Cache and Reload Window.
Andreas Violaris
  • 2,465
  • 5
  • 13
  • 26
akash joshi
  • 901
  • 6
  • 6
  • On a related note, if you import like this: from sqlalchemy import Column... It may complain about spelling. It provides a quick fix to update your .vscode/settings to make this word known. This, plus the much-appreciated suggestion above is what cleared it up for me. – val Mar 02 '23 at 18:55
  • That did it for me...Gracias Muchos denada – Eric Feurich Aug 03 '23 at 17:20
  • this was beautiful, thank you – lizziepika Aug 10 '23 at 21:30
62

If you are using a virtual environment, and even after trying pip installing all the necessary libraries, you have to select the python interpreter that exists in the virtual environment folder.

  1. (Ctrl+Shift+P) then search for "Python: Select Interpreter"
  2. Click "Enter interpreter path" followed by "Find.."
  3. Navigate to your project virtual environment folder
  4. Go into "Scripts" folder and then select "python.exe" as the interpreter.

These steps allow you to select the right python interpreter associated with the project's virtual environment.

DeeMeow
  • 701
  • 5
  • 7
11

I specified a path to the python interpreter I'm using within the settings.json file contained in the project repo's .vscode folder.

"python.pythonPath": "path-to-interpreter.python.exe"

Thanks to the following resource! https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b

Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
SMKrizan
  • 1,049
  • 1
  • 6
  • 10
  • 2
    Unlike the previous solutions - This worked for me, but more specifically I set `python.pythonPath` to the result of running `which python` FROM WITHIN the VSCode Integrated Terminal that is accessed via Ctrl-Shift+`. This is a different path than running it in a normal term window, as it is inside your virtual env. – Brian C Dec 08 '21 at 22:38
7

I ran into this error after an upgrade of my local python version (brew -> manual install), even though the specified interpreter was already /usr/local/bin/python3.

CMD + Shift + P and re-selecting the [same] interpreter fixed the error.

In hindsight, I suspect that a restart of VSCode could have also fixed this. ‍♂️

Adam Smooch
  • 1,167
  • 1
  • 12
  • 27
  • This worked for me. Only difference is that I manually entered my interpreter's path, since I was working on an external Linux machine. I simply typed `which python` in the terminal and copy-pasted the file location into the interpreter's path. – ixaixim Nov 17 '22 at 10:53
1

I was getting the very same error you have and none of the solutions presented here worked me. I work on a windows machine. I use miniconda to manage my virtual environments. And when I'm coding I launch every program from the command prompt (cmd), including Vscode. Even tough inside Vscode the python evironment was correctly settled I was getting the very same import error that you mention. The interesting part of it was that I wasn't having any problems to run the code, it was working as usuall. But when coding I wasn't able to see the functions related to the libraries with the import error.

The solution:

Activate the correct conda env for the project before launch Vscode from the command prompt.

Why does this happen?

I BELIVE that this happen because when you don't activate any conda env before launch Vscode. The base conda env is loaded as default. That generates the import conflict. (You can chek this out installing those packagues into the environment and running everything just as you have been doing)

Nicolás Muñoz
  • 128
  • 1
  • 7
0

Perhaps VSCode is using the incorrect Python path for this reason. A base interpreter should be used instead of the vscode interpreter, if necessary.

jeffasante
  • 2,549
  • 2
  • 8
  • 8
0

After verifying my Python interpreter was sourced correctly in VS Code, I simply cleaned my env and reinstalled the project locally and the import errors disappeared. I personally had an odd VS Code/Pylance cache and reinstalling the dependencies and modules fixed it for me.

Justin Hammond
  • 595
  • 8
  • 20
0

This might not have been relevant in your case but if you open an interactive pane in VS Code then VS Code will stick with that environment no matter what you set it to instead. So close the interactive window, switch away from and then back to the environment you want to use.

grofte
  • 1,839
  • 1
  • 16
  • 15
0

For VSCode, use the following command to install dependency packages into project virtual environment.

.venv\scripts\python -m pip install -r requirements.txt