Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10
Asked Answered
S

15

94

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?

Subtropics answered 22/7, 2021 at 14:2 Comment(0)
J
162
  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.

Julianajuliane answered 23/7, 2021 at 7:41 Comment(4)
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.Subtropics
"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.German
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.Ace
None of these instructions has worked for me so far. It's worth to be said I'm using the "Insiders" version of VSCode (I need it to make Copilot Chat work)Jameejamel
N
121

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.
Nutshell answered 24/10, 2022 at 5:57 Comment(1)
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.Drank
K
69

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.

Kappel answered 4/12, 2021 at 7:33 Comment(7)
This worked for me, but for me the interpreter I selected was bin/python (symlink to /usr/bin/python3)Carpophagous
Also worked for me, but in my case (on Linux) I'm using python installed using nix. So I first did which python to get the location of the python binary, and looked for that particular binary in the list of interpreters. That worked!Harrietharriett
This works but I worry about the fact that vscode says "Unknown configuration setting" when I hover over this.Trenna
script folder not showingLorenza
This worked, except I had to navigate to the path of which python in my venvClaytor
For poetry poetry show -v then you will find the path just copy and paste.Quadruplet
This worked for me, but for me the interpreter I selected the conda or venv folder where the packages were installedObservatory
S
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

Subtropics answered 23/7, 2021 at 22:42 Comment(1)
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.Deathlike
B
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. 🤷‍♂️

Buroker answered 15/9, 2022 at 14:42 Comment(1)
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.Senate
U
2

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 set, 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 usual. 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 launching Vscode from the command prompt.

Why does this happen?

I BELIEVE that this happens because when you don't activate any conda env before launching Vscode, the base conda env is loaded as default. That generates the import conflict (You can chek this out by installing those packages into the environment and running everything just as you have been doing).

Ultima answered 5/11, 2022 at 2:43 Comment(0)
F
2
  1. Run pip --version and copy the site-packages path. (don't take the whole pip path, just the folder location)
  2. Open settings on VS, and type @ext:ms-python.vscode-pylance on the settings search bar.
  3. Find the Python › Analysis: Extra Paths setting.
  4. Click Add Item and paste the copied value.
Fernandafernande answered 2/11, 2023 at 9:51 Comment(0)
D
2

After making sure my Python environment was set correctly, I did a View->Command Palette...->Developer: Reload Window

Apparently, in my case, pylance needed the window reloaded to get proper context.

Disparity answered 4/11, 2023 at 16:44 Comment(0)
F
1

Selecting the Python Interpreter and then Clearing cache with reload window worked for me.

Formula answered 8/12, 2023 at 12:55 Comment(0)
W
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.

Wellread answered 17/8, 2022 at 9:19 Comment(0)
I
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.

Ignatia answered 15/9, 2022 at 18:6 Comment(0)
C
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.

Collocation answered 27/6, 2023 at 9:47 Comment(0)
P
0

For VSCode, use the following command to install dependency packages into project's virtual environment:

.venv\scripts\python -m pip install -r requirements.txt
Padegs answered 2/7, 2023 at 6:39 Comment(0)
S
0

For miniconda, I had to install python-dotenv with conda install python-dot-env. I tried passing target flag to pip.

Saprolite answered 8/9, 2023 at 16:16 Comment(0)
U
-1

Using the windows option "connect to a network drive" I connected my computers to the place where I stored the file.

Now it works fine.

enter image description here

Unofficial answered 20/11, 2023 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.