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.

Problem with FastAPI and VSCODE
Frequent 'pylance' Questions - Stack Overflow
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
Hi everyone, im trying to learn FastAPI in school but when I try using "import FastAPI from fastapi" in the beggining of the code, it gives me an error as if I didnt have it downloaded. Can someone help? I already have all this extensions downloaded and im using a WSL terminal on Visual Studio Code.
annotated-doc==0.0.3
annotated-types==0.7.0
anyio==4.11.0
certifi==2025.10.5
click==8.3.0
dnspython==2.8.0
email-validator==2.3.0
fastapi==0.120.1
h11==0.16.0
httpcore==1.0.9
httptools==0.7.1
idna==3.11
Jinja2==3.1.6
markdown-it-py==4.0.0
MarkupSafe==3.0.3
mdurl==0.1.2
numpy==2.3.4
pyarrow==22.0.0
pydantic==2.12.3
pydantic_core==2.41.4
Pygments==2.19.2
pylance==0.38.3
python-dotenv==1.2.1
python-multipart==0.0.20
PyYAML==6.0.3
rignore==0.7.1
sentry-sdk==2.42.1
shellingham==1.5.4
sniffio==1.3.1
starlette==0.49.0
typing-inspection==0.4.2
typing_extensions==4.15.0
urllib3==2.5.0
uvicorn==0.38.0
uvloop==0.22.1
watchfiles==1.1.1
websockets==15.0.1
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.
No matter how many times I verify it, I always get the Module not found error. I've confirmed fastapi install and version with pip freeze. I have 0.109.0 installed. The interpretter is the same as the python/python3 --version. I've checked all the which python/python3 and they lead to same path. Any help is greatly appreciated. TIA.
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