VS Code flags an import as could not be resolved although it is installed
Your error
Import "pytest" could not be resolved Pylance(reportMissingImports)1 more line it can showFix it fast
Most common cause, 1 of 3VS Code has picked a different interpreter
Point VS Code at the venv that holds the package.
# Command Palette (Ctrl+Shift+P / Cmd+Shift+P)
Python: Select Interpreter
# then choose, for example: ./.venv/bin/pythonShort answer
Import "pytest" could not be resolved (reportMissingImports) means Pylance found no such module in the environment VS Code has selected. Usually the package sits in another venv or was never installed in the selected one. Pick the right interpreter in the status bar, install with that interpreter, and add your own source folders to extraPaths. Applies to Pylance 2026.4.
Which cause do you have?
In a VS Code terminal, print the prefix of the running Python and try the import there. Then click the Python version in the status bar and compare the path it shows with sys.prefix. Different paths point at cause 1, a failed import in the right environment at cause 2, and an import of your own code at cause 3.
python -c "import sys; print(sys.prefix)"
python -m pip show pytest
python -c "import pytest; print(pytest.__file__)"Then find the line you see in this list and open its fix:
- 1VS Code has picked a different interpreter
pytest imports fine in a terminal, but the status bar shows a global Python or a venv other than the one where you installed it - 2The package is not installed in the selected environment
With the right interpreter selected, python -m pip show pytest returns no Name or Version lines - 3The import is your own code, outside the paths Pylance searches
The unresolved name is a package from your repository (a src folder, or one installed with pip install -e .), and it runs fine at runtime
What does the error look like?
Import "pytest" could not be resolved Pylance(reportMissingImports)Import "mypackage" could not be resolved
Cause 1 of 3
VS Code has picked a different interpreter
You seepytest imports fine in a terminal, but the status bar shows a global Python or a venv other than the one where you installed it
Check
Most often Import could not be resolved appears because Pylance analyses code against the environment selected in VS Code, not the one active in your shell. If pytest lives in .venv and the status bar names the system Python, Pylance cannot see it, even though tests run from the terminal.
which python # macOS / Linux
where python # WindowsFix
Point VS Code at the venv that holds the package. Either click the Python version in the status bar, or open the Command Palette, type Python: Select Interpreter and choose that environment. The choice is also used for running and debugging.
# Command Palette (Ctrl+Shift+P / Cmd+Shift+P)
Python: Select Interpreter
# then choose, for example: ./.venv/bin/pythonVerify
The status bar now shows the project venv, and the squiggle under import pytest disappears after Pylance re-analyses the file.
python -c "import sys; print(sys.prefix)"Confirmed by code.visualstudio.com
Cause 2 of 3
The package is not installed in the selected environment
You seeWith the right interpreter selected, python -m pip show pytest returns no Name or Version lines
Check
When the selected environment really lacks pytest, Import "pytest" could not be resolved is simply correct. Pylance's own guide lists this as its own case: the interpreter is right, but the library was installed with another pip, with pipx, or into a different project's venv.
python -m pip show pytestFix
Install the package with the interpreter Pylance uses. Calling pip through python -m makes sure the install lands in that environment and not in whatever pip comes first on PATH.
python -m pip install pytest
# or with a dev requirements file:
python -m pip install -r requirements-dev.txtVerify
pip show now prints the pytest version and its location inside the venv.
python -m pip show pytestConfirmed by raw.githubusercontent.com
Cause 3 of 3
The import is your own code, outside the paths Pylance searches
You seeThe unresolved name is a package from your repository (a src folder, or one installed with pip install -e .), and it runs fine at runtime
Check
For your own modules, could not be resolved means Pylance does not know the import root. It treats the opened folder as the root, so code under src or another subfolder is missed. An editable install made by setuptools 64 or newer writes an import-hook .pth file by default, and Pylance skips such lines unless it can discover them on Python 3.13 and later.
find .venv/lib -name "*.pth" | xargs catFix
Add the source folder to python.analysis.extraPaths in .vscode/settings.json, or reinstall the package in setuptools compat mode so the .pth file holds a plain path.
// .vscode/settings.json
{
"python.analysis.extraPaths": ["./src"]
}
# or rebuild the editable install with a path-based .pth file:
python -m pip install -e . --config-settings editable_mode=compatVerify
The .pth file now contains a folder path instead of a line starting with import, and the import resolves in the editor.
find .venv/lib -name "*.pth" | xargs catConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Create the venv inside the project folder as .venv: the Python extension picks a workspace-local .venv before global interpreters when nothing is selected. Commit a requirements file that lists pytest.
- Install packages with python -m pip from the VS Code terminal rather than a bare pip command.
- Keep python.analysis.extraPaths in the workspace .vscode/settings.json for src layouts, so every machine resolves imports the same way.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.