# Fix VS Code IntelliSense Not Working After Virtual Environment Change

You switch your Python project to a new virtual environment, but VS Code's IntelliSense stops suggesting completions. The red squiggly lines appear under imports that exist in the new environment, and the status bar still shows the old interpreter path.

Step 1: Verify the Selected Interpreter

Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and run:

bash
Python: Select Interpreter

Check that the correct virtual environment is selected. The path should point to your new venv:

bash
/path/to/new-venv/bin/python

If your new environment does not appear in the list, click "Enter interpreter path" and paste the full path to the Python executable.

Step 2: Check Pylance Is Active

VS Code uses the Pylance language server for Python IntelliSense. Verify it is running:

  1. 1.Open the Output panel (View > Output)
  2. 2.Select "Python Language Server" from the dropdown
  3. 3.Look for errors during server initialization

If Pylance is not installed:

json
// In extensions, search for:
"ms-python.vscode-pylance"

Install it and reload VS Code.

Step 3: Configure python.pythonPath

In your workspace .vscode/settings.json:

json
{
    "python.defaultInterpreterPath": "./venv/bin/python",
    "python.analysis.extraPaths": [
        "./src",
        "./lib"
    ]
}

The python.defaultInterpreterPath (replacing the deprecated python.pythonPath) tells VS Code which Python environment to use for IntelliSense.

Step 4: Restart the Language Server

Sometimes the language server caches the old environment. Force a restart:

bash
Python: Restart Language Server

From the Command Palette. This clears the analysis cache and re-indexes the new environment's packages.

Step 5: Rebuild the Analysis Cache

If restarting does not help, delete the language server cache:

```bash # Linux/macOS rm -rf ~/.vscode/extensions/ms-python.vscode-pylance-*/dist/typeshed-fallback/

# Or clear VS Code's global storage rm -rf ~/.config/Code/User/globalStorage/ms-python.vscode-pylance/ ```

Then reload VS Code (Developer: Reload Window). The language server will rebuild its type stubs from the new environment.

Step 6: Check for Missing Type Stubs

Pylance uses type stubs (.pyi files) for IntelliSense on third-party packages. If a package lacks type information, IntelliSense will be incomplete:

bash
# Install type stubs for common packages
pip install types-requests types-redis types-PyYAML types-python-dateutil

Or configure Pylance to use basic type checking instead of strict:

json
{
    "python.analysis.typeCheckingMode": "basic"
}

Step 7: Workspace Folder Structure

If your project has a complex folder structure, Pylance may not find the source files. Configure extra paths:

json
{
    "python.analysis.extraPaths": [
        "${workspaceFolder}/src",
        "${workspaceFolder}/tests",
        "${workspaceFolder}/packages/mylib"
    ]
}

These paths are added to the analysis search path, allowing Pylance to find modules that are not in the Python path but are part of your project.

Step 8: Verify Import Resolution

Open a Python file and check the imports. Hover over an import to see where Pylance resolves it:

python
import requests
# Hover shows: /path/to/new-venv/lib/python3.11/site-packages/requests/__init__.py

If the resolution points to the old environment or a system-wide installation, the interpreter is not correctly configured.

Debugging IntelliSense Issues

Enable verbose logging:

json
{
    "python.analysis.logLevel": "Trace"
}

Then check the Output panel for the Python Language Server. The trace log shows every file Pylance indexes and every import it resolves. Look for lines like:

bash
[Info] Analysis cache path: /home/user/.cache/ms-python/python
[Info] Interpreter search paths:
[Info]     /path/to/new-venv/lib/python3.11/site-packages
[Info]     /path/to/new-venv/lib/python3.11

If the search paths do not include your new venv, the interpreter configuration is incorrect.