The causes could be:
- The name of the file -
fastapi.py, if you name it in this could way you would get import errors. (Avoid filenames similar to package names) - Installing on other environments. Most of the time it happens when you use both
CondaandPiPon you system. - If you run the code from VS Code, check if the
same interpreteris selected.
Verify the installation:
$ pip list | grep fastapi
Suggestions:
OPT Virtual Environments to avoid these troubles.
Thank you!
Answer from Mahimai Raja J on Stack OverflowThe causes could be:
- The name of the file -
fastapi.py, if you name it in this could way you would get import errors. (Avoid filenames similar to package names) - Installing on other environments. Most of the time it happens when you use both
CondaandPiPon you system. - If you run the code from VS Code, check if the
same interpreteris selected.
Verify the installation:
$ pip list | grep fastapi
Suggestions:
OPT Virtual Environments to avoid these troubles.
Thank you!
Addtional, don't forget using LazyVim extra Python config.
I have the same question when using LazyVim, the solution is venv.
python3 -m venv .venv
source .venv/bin/activate
pip install fastapi
After these commands, the fastapi is correctly imported with LSP.

python - how do I solve the Pylance(reportMissingImports)? - Stack Overflow
flask - Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10 - Stack Overflow
Import could not be resolved [Pylance]
Frequent 'pylance' Questions - Stack Overflow
Videos
If you want to install a package into your specific virtual environment, you need to first "activate" that environment. Likewise, if you want to run your script in that environment, you need to first "activate" it. You can do this manually or preferably let VSCode handle it for you.
In order to tell VSCode(especially the language server which is pylance) to use that environment:
- Open up your Command Palette(press
ctrl+shift+Porf1) and type :"python: select interpreter". - Browse/Select your newly created python interpreter's path inside your venv.
- Add
"python.terminal.activateEnvironment": trueto yoursetting.jsonfile(It's also possible via GUI setting of course by searching forpython: activate environment). This will automatically detect and activate your venv whenever you open your integrated terminal. Obviously If the path should points to a venv interpreter. (Note: you have to have a Python file opened in your editor).
You can also set your Python's interpreter path manually:
- Create a folder called
.vscodein your workspace. - Create a file called
settings.jsoninside it. - Add these to
settings.json:
{
"python.defaultInterpreterPath": "PATH_TO_VENV_INTERPRETER",
"python.terminal.activateEnvironment": true
}
Note: What I normally do is, I insert a "python.defaultInterpreterPath" key to my User settings.json which points to my global interpreter. Then I create Workspace settings.json for each of my projects and add the same key which points to my venv's interpreter. Remember, workspace settings.json will overwrite user's settings.json.
This way whenever you open VSCode in a project folder it automatically knows it should activate your venv's interpreter(I told it to do so with "python.terminal.activateEnvironment") and if you open VSCode in a normal folder it correctly use your global's interpreter.
Difference between User and Workspace settings.json.
Please select the appropriate interpreter for your running environment.
ctrl+shift+P
and then choose the one like picture.

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.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.
Install
sqlalchemyandmongoenginewith commandpip install. Once installing them successfully, there will intellisense when you import them and no warnings shown.

Besides, the folder .vscode is to store Workspace settings as well as debugging and task configurations.
To resolve the issue, perform the following steps:
- Open the Command Palette by pressing Ctrl+Shift+P on your keyboard.
- In the Command Palette, select Python: Clear Cache and Reload Window.
I'm trying to use torch in a python script but even though it's pip installed, pylance doesn't recognize it
https://imgur.com/EM5fEIo
Any advice on how to resolve this? Thanks.
Pylance, by default, includes the root path of your workspace. If you want to include other subdirectories as import resolution paths, you can add them using the python.analysis.extraPaths setting for the workspace.
- In VS Code press
<ctrl> + <,>to open Settings. - Type in
python.analysis.extraPaths - Select "Add Item"
- Type in the path to your library
/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/
Two methods below:
In VS code you can edit the
setting.jsonfile. If you add"python.analysis.useImportHeuristic": truethe linting error will be removed.The alternative is to add
# type: ignoreat the end of the import code.
Here is the github link that i got the above resolution from: https://github.com/microsoft/pylance-release/issues/68
It worked for me: python 3.9, VScode, windows10
I'm getting this weird thing in vscode where my import is working properly but theres a yellow line under the module name and when I hover over that, it says module can't be resolved.
If I try and change how I access the module (using a relative path, perhaps), the yellow error line is no longer there but the code doesn't work as python or vscode doesn't recognise the module name/path
Does anyone have experience with this?
My code works but the yellow line is annoying.
I don't NEED to separate my code into different files but it would still be good to know what's going on here
Thank you