The causes could be:

  1. 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)
  2. Installing on other environments. Most of the time it happens when you use both Conda and PiP on you system.
  3. If you run the code from VS Code, check if the same interpreter is 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 Overflow
🌐
Sentry
sentry.io › sentry answers › vs code › fix pylance resolvemissingimports in vs code
Fix Pylance resolveMissingImports in VS Code | Sentry
July 15, 2024 - If you previously installed fastapi to the virtual environment, the resolveMissingImports error should now disappear.
Discussions

python - how do I solve the Pylance(reportMissingImports)? - Stack Overflow
I used pip install fastapi to download it in my virtual environment, in terminal but when I try to import from it it shows up as a missing import. More on stackoverflow.com
🌐 stackoverflow.com
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
My Flask App server is running but I have three imports that cannot be resolved. I have tried: reinstalling the imports individually reinstalling requirements.txt I configured VSCode Workspace wi... More on stackoverflow.com
🌐 stackoverflow.com
Import could not be resolved [Pylance]
Try to Reload Window. If the error still exists, check if you install the module in your selected interpreter environment. More on reddit.com
🌐 r/vscode
12
14
June 23, 2021
Frequent 'pylance' Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
31

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:

  1. Open up your Command Palette(press ctrl+shift+P or f1) and type : "python: select interpreter".
  2. Browse/Select your newly created python interpreter's path inside your venv.
  3. Add "python.terminal.activateEnvironment": true to your setting.json file(It's also possible via GUI setting of course by searching for python: 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:

  1. Create a folder called .vscode in your workspace.
  2. Create a file called settings.json inside it.
  3. 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.

2 of 2
6

Please select the appropriate interpreter for your running environment.

ctrl+shift+P

and then choose the one like picture.

🌐
Stack Overflow
stackoverflow.com › questions › tagged › pylance
Frequent 'pylance' Questions - Stack Overflow
I've been searching online for quite a while now and can't seem to find a solution for my problem. I installed Pylance (the newest Microsoft interpreter for Python) and can't seem to disable linting ... ... I used pip install fastapi to download it in my virtual environment, in terminal but when I try to import from it it shows up as a missing import.
Find elsewhere
🌐
GitHub
github.com › microsoft › pylance-release › issues › 4606
Pylance seems to forget the selected interpreter after a (short) while: reportMissingImports · Issue #4606 · microsoft/pylance-release
July 14, 2023 - After some editing, lots of problems are shown because the imports could not be resolved. ... However, Pylance trace output shows that (at least part of) Pylance is still using the standard interpreter of Ubuntu.
Author   janfrederik
🌐
GitHub
github.com › microsoft › pylance-release › issues › 236
Import "[module]" could not be resolvedPylance (reportMissingImports) · Issue #236 · microsoft/pylance-release
August 13, 2020 - Import "a" could not be resolved · However, module "a" is really imported and it works well. If I delete "python.languageServer": "Pylance" and use Jedi, yellow wavy line won't show up. In addition, if i "open by code" in "chapter1" folder, yellow wavy line won't show up.
Author   jiangzhuochi
🌐
GitHub
github.com › microsoft › pylance-release › issues › 1443
Pylance does not update on adding new files and packages · Issue #1443 · microsoft/pylance-release
June 13, 2021 - Whenever I install a package or add, change the name of a file in the project directory pylance does not update. It show import error and autocompletion and intellisense does not work.
Author   Trance-Paradox
🌐
Medium
medium.com › @littechie › troubleshooting-import-module-error-in-python-a-solution-that-worked-901e2e2efb1b
Troubleshooting “Import Module” Error in Python: A Solution That Worked | by Lit Techie | Medium
July 26, 2023 - By creating a fresh virtual environment and reinstalling the required dependencies, I successfully resolved the “Import ‘module’ could not be resolved from source PylancereportMissingModuleSource” error that was preventing me from importing ...
🌐
DEV Community
dev.to › climentea › how-to-solve-pylance-missing-imports-in-vscode-359b
How to solve Pylance 'missing imports' in vscode - DEV Community
February 3, 2021 - Here is how you can solve this issue: Make sure you selected the right python interpreter for your...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-import-could-not-be-resolved-from-source-pylance-reportmissingmodulesource
Import "X" could not be resolved from source Pylance [Fixed] | bobbyhadz
April 10, 2024 - The error "Import "X" could not be resolved from source Pylance" occurs when the imported module is not installed or you have selected the incorrect Python interpreter in your IDE (e.g.
🌐
DEV Community
dev.to › drsimplegraffiti › fast-api-part-1--j2l
Fast Api [part 1 ] - DEV Community
March 31, 2022 - from typing import Optional from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"} class Person(BaseModel): name: str age: int | None = None tall: bool = False @app.post("/persons") async def create_person(person: Person): return {"data": f"my name is {person.name.upper()} and i am {person.age} years old"}
🌐
Stack Overflow
stackoverflow.com › questions › tagged › pylance
Highest scored 'pylance' questions - Stack Overflow
When running ipynbs in VS Code, I've started noticing Pylance warnings on standard library imports. I am using a conda virtual environment, and I believe the warning is related to that. An example ... ... whenever I try to import matplotlib or matplotlib.pyplot in VS Code I get the error in the title: Import "matplotlib" could not be resolved from source Pylance(reportMissingModuleSource) or ...
🌐
Visual Studio Code
code.visualstudio.com › docs › python › tutorial-fastapi
FastAPI Tutorial in Visual Studio Code
November 3, 2021 - This is because Pylance is built on top of Pyright, a static type checker for Python that can detect type errors in your code to prevent bugs and improve code quality. The three steps below are optional, but given that FastAPI uses type hints extensively to improve code readability and validation, we can take advantage of Pylance's type checking features to catch errors early on:
🌐
GitHub
github.com › microsoft › pylance-release › issues › 774
The auto-import logic is not picking the shortest import path for symbols · Issue #774 · microsoft/pylance-release
December 26, 2020 - Python version (& distribution if applicable, e.g. Anaconda): 3.7 in a venv with a fresh FastAPI installation.
Author   tiangolo
🌐
The Fish Shell
mvolkmann.github.io › blog › python › python-compared-to-js
Python compared to JavaScript
from fastapi import FastAPI, Response, status from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from typing import Optional import time import uuid # JSON in request bodies of POST and PUT requests # is validated against this type definition.
🌐
Reddit
reddit.com › r/learnpython › vscode saying import could not be resolved but it definitely is
r/learnpython on Reddit: vscode saying import could not be resolved but it definitely is
July 9, 2021 -

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