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

22,806

Solution 1

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

2.Run Terminal: Create New Integrated Terminal (Ctrl+Shift+`)) 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.

enter image description here

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

Solution 2

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.

Solution 3

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

Share:
22,806
Admin
Author by

Admin

Updated on December 04, 2021

Comments

  • Admin
    Admin over 2 years

    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?

  • Admin
    Admin almost 3 years
    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.
  • influent
    influent over 2 years
    "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.
  • Brian C
    Brian C over 2 years
    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.
  • jpoesen
    jpoesen over 2 years
    This worked for me, but for me the interpreter I selected was bin/python (symlink to /usr/bin/python3)