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:

bash
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.defaultInterpreterPath in .vscode/settings.json references the old venv
  • Workspace settings override the new interpreter selection

Step-by-Step Fix

  1. 1.Select the correct Python interpreter:
  2. 2.- Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
  3. 3.- Type "Python: Select Interpreter"
  4. 4.- Choose the new virtual environment from the list
  5. 5.- If not listed, click "Enter interpreter path" and navigate to the new venv's Python binary
  6. 6.Restart the Pylance language server:
  7. 7.- Press Ctrl+Shift+P
  8. 8.- Type "Python: Restart Language Server"
  9. 9.- Wait for the status bar to show "Pylance" with no loading indicator
  10. 10.Update workspace settings to use the new interpreter. In .vscode/settings.json:
  11. 11.```json
  12. 12.{
  13. 13."python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
  14. 14."python.languageServer": "Pylance",
  15. 15."python.analysis.extraPaths": [
  16. 16."${workspaceFolder}/src"
  17. 17.]
  18. 18.}
  19. 19.`
  20. 20.Verify the environment has required packages:
  21. 21.```bash
  22. 22.source .venv/bin/activate
  23. 23.pip list | grep -E "flask|django|numpy"
  24. 24.`
  25. 25.Install missing packages:
  26. 26.```bash
  27. 27.pip install -r requirements.txt
  28. 28.`
  29. 29.Clear Pylance cache if the issue persists:
  30. 30.```bash
  31. 31.# On Linux/macOS
  32. 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.json with relative paths (${workspaceFolder}/.venv/bin/python) so the settings work across machines
  • Keep a requirements.txt or Pipfile in 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 .venv to 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 pyenv or conda for more reliable environment management