Introduction
After switching Python virtual environments, VS Code's IntelliSense (powered by Pylance) may stop providing autocomplete, type hints, and error detection. The editor shows red underlines for all imports and offers no suggestions:
Import "flask" could not be resolved Pylance(reportMissingImports)This happens because the language server is still referencing the old virtual environment's package paths.
Symptoms
- All imports show "could not be resolved" errors
- No autocomplete suggestions for Python code
- Go to Definition does not work
- IntelliSense works for built-in modules but not for installed packages
- Python extension shows the wrong interpreter in the status bar
Common Causes
- VS Code Python extension still pointing to the old virtual environment path
- Pylance language server has not been restarted after environment change
- The new virtual environment was created outside the workspace
python.defaultInterpreterPathin.vscode/settings.jsonreferences the old venv- Workspace settings override the new interpreter selection
Step-by-Step Fix
- 1.Select the correct Python interpreter:
- 2.- Press
Ctrl+Shift+P(orCmd+Shift+Pon Mac) - 3.- Type "Python: Select Interpreter"
- 4.- Choose the new virtual environment from the list
- 5.- If not listed, click "Enter interpreter path" and navigate to the new venv's Python binary
- 6.Restart the Pylance language server:
- 7.- Press
Ctrl+Shift+P - 8.- Type "Python: Restart Language Server"
- 9.- Wait for the status bar to show "Pylance" with no loading indicator
- 10.Update workspace settings to use the new interpreter. In
.vscode/settings.json: - 11.```json
- 12.{
- 13."python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
- 14."python.languageServer": "Pylance",
- 15."python.analysis.extraPaths": [
- 16."${workspaceFolder}/src"
- 17.]
- 18.}
- 19.
` - 20.Verify the environment has required packages:
- 21.```bash
- 22.source .venv/bin/activate
- 23.pip list | grep -E "flask|django|numpy"
- 24.
` - 25.Install missing packages:
- 26.```bash
- 27.pip install -r requirements.txt
- 28.
` - 29.Clear Pylance cache if the issue persists:
- 30.```bash
- 31.# On Linux/macOS
- 32.rm -rf ~/.vscode/extensions/ms-python.vscode-pylance-*/dist/typeshed-fallback/stdlib
# Then restart VS Code completely code --disable-extensions code ```
Prevention
- Use
.vscode/settings.jsonwith relative paths (${workspaceFolder}/.venv/bin/python) so the settings work across machines - Keep a
requirements.txtorPipfilein your project root for reproducible environments - Always run "Python: Select Interpreter" after creating or switching virtual environments
- Use Python extension's auto-detection feature:
"python.venvPath": "${workspaceFolder}" - Add
.venvto your workspace's trusted folders to avoid permission issues - Test IntelliSense after environment changes by opening a file with known imports
- Document the expected Python version and key packages in your project README
- Consider using
pyenvorcondafor more reliable environment management