16

I keep getting error "X" could not be resolved Pylance(reportMissingImports) [ln 1, Col8]

I AM in fact a beginner, the basic youtube "fixes" are NOT working

  • View Command Pallet ... NOT working
  • Terminal pip install .... NOT working I am running the Zip install on my work computer and im guessing it has something to do with a directory. but i cant seem to figure it out. the bottom left corner shows the python version which is ( Pyhton 3.110a7 64-bit(windows store)
Prabhat Mcdonnough
  • 171
  • 1
  • 1
  • 3
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Apr 19 '22 at 03:54
  • 1
    Hi, you may have more than one python installed, have you selected the right one for your project? In the footer of VSCode the version of python it thinks you are using will be displayed. Click that and a drop down will appear from the selection box in the top bar of the code window, select the appropriate python and then wait a few minutes for it to scan your code and find all of your imports. – mike Nov 29 '22 at 18:51

10 Answers10

38

Pylance requires you to set the Python PATH:

If you're in Mac/linux, make use of :

which python3

And in windows:

where python

So that the path in which you're python is installed is returned

Copy that path.

Go to your vscode and open the settings.json file (CTRL + SHIFT + P, and type "settings.json" at search bar)

Add the following key to the json file

"python.defaultInterpreterPath": "/Users/YOURUSERNAME/opt/anaconda3/bin/python3"

This was just an example, the PATH could be something more like "C:/users/YOURUSERNAME/anaconda3/bin/python3" in case you're using windows.

The following documentation from python for vscode provides more information about how to configure Python for Visual Studio Code: https://code.visualstudio.com/docs/python/settings-reference

alramdein
  • 810
  • 2
  • 12
  • 26
  • 3
    This was a huge help and solved my issue perfectly. One minor note is that "where python" didn't work for me on windows and instead I had to run import sys; locate_python = sys.exec_prefix; print(locate_python) in the interpreter to find the python location. – cfn May 25 '22 at 10:49
  • This fixed it for using venv in macos despite activating my using source env_name/bin/activate – Yugendran Jan 12 '23 at 07:57
  • This works perfectly in VSCode ... now also a working version for VS Studio would be helpful in addition. – PWillms Feb 11 '23 at 16:11
7

There’s a very comprehensive discussion on settings in VS Code here: https://stackoverflow.com/a/63211678/5709144.

In summary and with regard to this specific case, it’s better to change settings by going to (on a Mac) Code > Preferences > Settings.

Enter python.defaultInterpreterPath in the search box at the top of screen. The current path is shown in an editable text box.

Enter any path you like here - foobar, mother, whatever you like. It doesn’t matter as, if the path isn’t recognised by VS Code, VS Code lists those that are. The only reason you enter the path is to get the list of potential paths. Click on one of these accepted paths and you’re all set up.

amunnelly
  • 1,144
  • 13
  • 22
4

As asked, your question doesn't specify whether or not your imported module is correctly installed. If it isn't, then this answer will not apply. However, if your code works as expected and you're getting a false warning, then you can ignore the warning by doing the following.

Create the file .vscode/settings.json in your current directory and then add the following:

"python.analysis.diagnosticsSeverityOverrides": {
    "reportMissingImports": "none",
}

Be warned however that this will ignore all missing import warnings, not just the one you're trying to get rid of. Therefore, if you have any imports that are legitimately missing, the warning will not be there.

piccoloser
  • 181
  • 1
  • 10
3

Also, on some occasions, you might have configured your environment by adding custom paths that Pylance can not detect.

In that case, you can use the python.analysis.extraPaths parameter to add more paths to your project, such as :

"python.analysis.extraPaths": ["app", "another/path/etc"]

(Source: https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b)

Cyril N.
  • 38,875
  • 36
  • 142
  • 243
1

If you are using the VS, Please go to the settings .. search for Advance path and then ADD it (/.source) it should solve the problem.

Hope you have installed the Pylance in your system correctly.

Akash Ray
  • 11
  • 2
1

Many of the solutions found were not effective fx."python.analysis.useImportHeuristic": true.

It worked to create a settings.json file within the workspace - CTRL+SHIFT+P and search for Preferences: Open Workspace Settings(JSON). A new folder .vscode is created to store the new settings of such specific workspace.

There, you insert the extraPath "python.analysis.extraPaths": [".venv/projectfolder/src"]

In my case, I have placed the project folder within the virtual environment and I have a source folder src, where it is present a main.py with which I run my script and the pylance import errors were reported

  • This is the way. In my case (Mac with homebrew python, OG virtualenvwrapper and virtualenv) the path was `"/Users/airstrike/.virtualenvs/NAMEOFVENVGOESHERE/lib/python3.11/site-packages"` – airstrike Aug 30 '23 at 21:19
0

For me, I'm editing the .py file over SFTP, and my local don't have those required library, you can click [Ctrl] + [Shift] + [P], and search for Open Workspaces Settings (JSON), and here's my setting for your reference

{
    "python.analysis.diagnosticSeverityOverrides": {
        "reportMissingImports": "none",
        "reportMissingModuleSource": "none"
    }
}

Reference: https://code.visualstudio.com/docs/python/settings-reference

Samuel Chan
  • 231
  • 1
  • 4
  • 11
0

In summary and with regard to this specific case, it’s better to change settings by going to (on a Mac) Code > Preferences > Settings.

Enter python.defaultInterpreterPath in the search box at the top of screen. The current path is shown in an editable text box. Enter Python3

Next scroll down and find Python> Analysis: import format and check Relative its my be help you.

Maks
  • 1
  • 3
0

In my case, I had created a virtual env one directory above the project. Just make it in the same directory and restart the virtual studio code and activate the environment. *make sure to install the package again.

Worked for me!!

-3

At the bottom-right corner of VS Code:

down right corner of VScode

Just switch on these TypeChecking braces.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Abu Bakar
  • 1
  • 1