Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 240
SMKrizan Avatar asked Jul 22 '21 14:07

SMKrizan


4 Answers

  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.

like image 105
Molly Wang-MSFT Avatar answered Oct 11 '22 06:10

Molly Wang-MSFT


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.

like image 44
DeeMok Avatar answered Oct 11 '22 07:10

DeeMok


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

like image 44
SMKrizan Avatar answered Oct 11 '22 06:10

SMKrizan


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.

I suspect that a restart of VSCode may have also fixed this, in the end.

like image 3
Adam Smooch Avatar answered Oct 11 '22 07:10

Adam Smooch