Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

VS Code has picked a different interpreter

Point VS Code at the venv that holds the package.

Fix
# Command Palette (Ctrl+Shift+P / Cmd+Shift+P)
Python: Select Interpreter
# then choose, for example: ./.venv/bin/python
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to Pylance 2026.4.1 (VS Code Python extension), Python 3.12 to 3.14, pytest 9.2.0, setuptools 84.2.1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1VS Code has picked a different interpreterpytest imports fine in a terminal, but the status bar shows a global Python or a venv other than the one where you installed it
  2. 2The package is not installed in the selected environmentWith the right interpreter selected, python -m pip show pytest returns no Name or Version lines
  3. 3The import is your own code, outside the paths Pylance searchesThe 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.

Check
which python        # macOS / Linux
where python        # Windows

Fix

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.

Fix
# Command Palette (Ctrl+Shift+P / Cmd+Shift+P)
Python: Select Interpreter
# then choose, for example: ./.venv/bin/python

Verify

The status bar now shows the project venv, and the squiggle under import pytest disappears after Pylance re-analyses the file.

Verify
python -c "import sys; print(sys.prefix)"

Confirmed by code.visualstudio.com

Did this fix it?

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.

Check
python -m pip show pytest

Fix

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.

Fix
python -m pip install pytest
# or with a dev requirements file:
python -m pip install -r requirements-dev.txt

Verify

pip show now prints the pytest version and its location inside the venv.

Verify
python -m pip show pytest

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
find .venv/lib -name "*.pth" | xargs cat

Fix

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.

Fix
// .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=compat

Verify

The .pth file now contains a folder path instead of a line starting with import, and the import resolves in the editor.

Verify
find .venv/lib -name "*.pth" | xargs cat

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.