Skip to content

Instantly share code, notes, and snippets.

@zepadovani
Last active October 3, 2025 00:13
Show Gist options
  • Select an option

  • Save zepadovani/f90e99844db676112151c69468cad4b3 to your computer and use it in GitHub Desktop.

Select an option

Save zepadovani/f90e99844db676112151c69468cad4b3 to your computer and use it in GitHub Desktop.
VS Code: Configure AI Agents to Use Correct Python Environment Automatically

Fix AI Agent Python Environment in VS Code

Problem

AI agents in VS Code use the wrong Python environment:

  • Manual terminals: Use configured project environment
  • AI agent terminals: Default to system base environment

Root Cause

AI agents request terminals directly from VS Code core, bypassing the Python extension's automatic environment activation.

Solution

Configure a custom terminal profile in .vscode/settings.json:

{
    "python.defaultInterpreterPath": "/path/to/conda/envs/myenv/bin/python",
    "python.terminal.activateEnvironment": true,
    "python.terminal.activateEnvInCurrentTerminal": true,
    "terminal.integrated.profiles.osx": {
        "myenv": {
            "path": "/bin/zsh",
            "args": ["-c", "source /path/to/conda/etc/profile.d/conda.sh && conda activate myenv && exec zsh"]
        }
    },
    "terminal.integrated.defaultProfile.osx": "myenv",
    "terminal.integrated.env.osx": {
        "CONDA_DEFAULT_ENV": "myenv",
        "CONDA_PREFIX": "/path/to/conda/envs/myenv",
        "PATH": "/path/to/conda/envs/myenv/bin:${env:PATH}",
        "CONDA_AUTO_ACTIVATE_BASE": "false"
    }
}

Platform Adjustments

Replace .osx with:

  • macOS: .osx
  • Windows: .windows
  • Linux: .linux

Key Components

  1. Custom Profile: terminal.integrated.profiles.osx.myenv creates a profile that auto-activates conda
  2. Default Profile: terminal.integrated.defaultProfile.osx makes it the default for ALL terminals
  3. Args Chain: Sources conda, activates environment, starts interactive shell

How It Works

The args array ensures every terminal:

  1. Loads conda functions
  2. Activates your environment
  3. Starts normal interactive shell

Verification

Test in fresh terminal:

echo $CONDA_DEFAULT_ENV  # Should show: myenv
python --version
which python  # Should point to your environment

Troubleshooting

  1. Restart VS Code after configuration changes
  2. Verify all paths are correct for your system
  3. Ensure conda command works in your shell
  4. Use correct platform suffix (osx/windows/linux)

Benefits

  • Project-specific configuration
  • Works for both manual and AI agent terminals
  • No global system changes required
  • Portable with your project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment