Skip to content

How to Auto-Activate Virtual Environment in VS Code for Python (Complete Guide)

Problem

Every time I opened a new terminal in VS Code, I had to type the same command:

terminal
source .venv/bin/activate

I did this dozens of times per day. Open a terminal, activate venv. Split terminal, activate venv. Close terminal by accident, open new one, activate venv again.

One day I forgot. I ran:

terminal
pip install requests pandas numpy

Then later I ran my script in another terminal where the venv was active:

terminal
$ python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'

Confused, I checked my installed packages:

terminal
$ pip list | grep requests
# Nothing...

But I just installed them! Oh. I installed them globally in one terminal while my venv was inactive. Classic mistake that cost me 20 minutes of debugging.

I went to Reddit and found others with the same frustration. The top comment: “VS Code can auto-activate your venv. You just need to select the right interpreter.”

Wait, what? I had been manually activating for years.

What’s Really Happening

The problem is not that VS Code lacks auto-activation. It’s that most developers don’t configure it properly.

VS Code’s Python extension has a setting called python.terminal.activateEnvironment. It’s enabled by default. But here’s the catch: VS Code needs to know WHICH virtual environment to activate. If you never selected an interpreter, VS Code has no idea.

The Missing Link
┌─────────────────────────────────────────────────────────────────┐
│ Why Manual Activation Feels Necessary │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Developer creates venv: │
│ python -m venv .venv │
│ │
│ Developer opens VS Code terminal: │
│ Prompt shows: user@machine:~/project$ │
│ No (.venv) prefix │
│ │
│ Developer thinks: "VS Code doesn't auto-activate!" │
│ Developer manually runs: source .venv/bin/activate │
│ │
│ Reality: VS Code was never told which venv to use │
│ The interpreter was never selected │
│ │
└─────────────────────────────────────────────────────────────────┘

The Reddit thread had a highly-upvoted alternative suggestion too: “Just use uv run. It handles everything automatically without needing to activate.”

The Solution: VS Code Auto-Activation

Let me fix my setup properly.

Step 1: Create a Virtual Environment

terminal
# Create venv in project directory
python -m venv .venv
# Or using uv (faster)
uv venv

I use .venv as the name because it’s the convention VS Code expects.

Step 2: Select the Interpreter in VS Code

This is the step I missed for years.

I opened the Command Palette with Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows/Linux). Then I typed:

Command Palette
Python: Select Interpreter

VS Code showed me a list of detected Python interpreters:

Interpreter List
> Python 3.12.0 64-bit ('.venv': venv) ← Recommended
Python 3.12.0 64-bit
Python 3.11.5 64-bit
+ Enter interpreter path...

I selected the one with .venv in the name. The status bar at the bottom of VS Code changed to show Python 3.12.0 ('.venv': venv).

Step 3: Verify Auto-Activation is Enabled

I opened VS Code settings with Cmd+, (Mac) or Ctrl+, (Windows/Linux). I searched for:

Settings Search
python.terminal.activateEnvironment

The setting was checked. This is the default, so I didn’t need to change anything.

Step 4: Test It

I opened a new terminal with Ctrl+Shift+`` (backtick). The prompt immediately showed:

terminal
(.venv) user@machine:~/project$

The (.venv) prefix appeared automatically. No manual activation needed.

Let me verify:

terminal
$ which python
/home/user/project/.venv/bin/python
$ pip list
Package Version
---------- -------
pip 24.0

My virtual environment was active from the moment the terminal opened.

How It Works

Here’s what happens behind the scenes:

Auto-Activation Flow
┌─────────────────────────────────────────────────────────────────┐
│ Terminal Opens in VS Code │
└───────────────────────────┬─────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ Python Extension Checks Settings │
│ │
│ python.terminal.activateEnvironment: true ✓ │
│ python.defaultInterpreterPath: .venv/bin/python ✓ │
└───────────────────────────┬─────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ Extension Sources Activate Script │
│ │
│ Unix: source .venv/bin/activate │
│ Windows: .venv\Scripts\activate.bat │
└───────────────────────────┬─────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ Terminal Prompt Shows: │
│ (.venv) user@machine:~/project$ │
└─────────────────────────────────────────────────────────────────┘

VS Code’s Python extension detects when a terminal opens, checks your selected interpreter, and runs the appropriate activation command for your platform. You never see it happen.

Alternative: Workspace Configuration

For team consistency, I can configure this in the project settings:

.vscode/settings.json
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.terminal.activateEnvironment": true
}

Now anyone who opens this project in VS Code automatically uses the correct venv. The ${workspaceFolder} variable ensures the path works regardless of where the project is located.

On Windows, the path is slightly different:

.vscode/settings.json (Windows)
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/Scripts/python.exe"
}

Alternative: The uv Approach

Remember the Reddit suggestion about uv run? Let me try that approach too.

terminal
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create a project
uv init my-project
cd my-project
# Add dependencies
uv add requests pandas numpy
# Run any Python command - no activation needed
uv run python main.py

The uv run command handles the virtual environment internally. I never think about activation at all.

terminal
# Open a new terminal? Just run:
$ uv run python script.py
# Works immediately, no activation
# Want a shell with venv active?
$ uv run bash
(.venv) $ # Now in activated environment

This is the modern approach. uv manages environments as an implementation detail, not a user responsibility.

Why I Didn’t Know About This

I realized why I missed this for years:

  1. I created venvs but never selected interpreters: VS Code can’t auto-activate what it doesn’t know about.

  2. I assumed it was complicated: The solution is literally one Command Palette action.

  3. I got used to the manual workflow: After typing source .venv/bin/activate hundreds of times, it became muscle memory. I stopped questioning it.

  4. The setting is hidden: python.terminal.activateEnvironment works silently in the background. There’s no UI indicator that auto-activation is happening.

Common Mistakes

Mistake 1: Creating venv Outside the Workspace

terminal
# WRONG - venv outside project folder
$ cd ~/virtualenvs
$ python -m venv myproject-env
# VS Code can't find it automatically

The venv should be inside the project:

terminal
# CORRECT - venv inside project
$ cd ~/projects/myproject
$ python -m venv .venv

Mistake 2: Using Different venv Names

terminal
# Inconsistent names across projects
project-a/
├── venv/ # One project uses 'venv'
project-b/
├── env/ # Another uses 'env'
project-c/
├── .venv/ # Another uses '.venv'

This causes confusion. Stick with .venv:

terminal
# CONSISTENT - always use .venv
project-a/
├── .venv/
project-b/
├── .venv/
project-c/
├── .venv/

Mistake 3: Not Restarting VS Code After Creating venv

terminal
# Create venv
$ python -m venv .venv
# Immediately try to select interpreter in VS Code
# → .venv might not appear in list yet

After creating a new venv, I either:

  • Reload the VS Code window (Cmd+Shift+P → “Developer: Reload Window”)
  • Or wait a few seconds for the Python extension to detect it

Mistake 4: Disabling the Setting Accidentally

While exploring VS Code settings, I once unchecked python.terminal.activateEnvironment thinking I didn’t need it. Then spent days wondering why auto-activation stopped working.

To fix:

.vscode/settings.json
{
"python.terminal.activateEnvironment": true
}

Or in the UI: Settings → search “activateEnvironment” → check the box.

Mistake 5: Wrong Windows Path Format

On Windows, the activate script location differs:

terminal
# WRONG on Windows
source .venv/bin/activate # This is Unix path
# CORRECT on Windows
.venv\Scripts\activate

VS Code handles this automatically when you select the interpreter. But if you manually configure paths, use the correct format.

Troubleshooting

Auto-Activation Not Working

If the terminal opens without (.venv) prefix:

terminal
# Check 1: Is the interpreter selected?
# Look at VS Code status bar (bottom). Should show:
# Python 3.x.x ('.venv': venv)
# Check 2: Is the setting enabled?
$ code --list-settings | grep activateEnvironment
python.terminal.activateEnvironment: true
# Check 3: Does the venv exist?
$ ls .venv/bin/activate
.venv/bin/activate # Should exist

Multiple venvs Confusion

If VS Code selects the wrong venv:

terminal
# List all detected interpreters
# Cmd+Shift+P → "Python: Select Interpreter"
# Or specify in settings.json
echo '{"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python"}' > .vscode/settings.json

Terminal Shows Different Python

terminal
$ which python
/usr/bin/python # Wrong! System Python
# Fix: Manually select interpreter in VS Code
# Cmd+Shift+P → "Python: Select Interpreter" → Choose .venv

Time Savings

Let me calculate what auto-activation saves:

Time Analysis
Manual activation per terminal: 5 seconds
Terminals opened per day: 15
Time saved per day: 75 seconds
Working days per year: 250
Time saved per year: 5.2 hours
Plus: Zero "installed to wrong environment" mistakes
Plus: Zero "why can't I import my package" debugging sessions

5 hours per year saved, plus fewer mistakes. All from one configuration step.

Which Approach Should You Use?

Decision Guide
┌─────────────────────────────────────────────────────────────────┐
│ If you want minimal changes: │
│ Use VS Code auto-activation (select interpreter once) │
│ │
│ If you want zero venv management: │
│ Use uv run (eliminates activation concept entirely) │
│ │
│ If you work on a team: │
│ Add .vscode/settings.json to version control │
│ Teammates get consistent interpreter on clone │
│ │
│ If you use multiple terminals heavily: │
│ uv run is cleaner (no activation in any terminal) │
└─────────────────────────────────────────────────────────────────┘

Summary

VS Code has built-in virtual environment auto-activation. The feature exists and is enabled by default. The missing step for most developers is simply selecting the interpreter.

The complete setup:

quickstart.sh
# 1. Create venv
python -m venv .venv
# 2. In VS Code: Cmd+Shift+P → "Python: Select Interpreter"
# Choose .venv/bin/python
# 3. Open new terminal
# Prompt shows: (.venv) automatically

Or for the modern approach with uv:

quickstart-uv.sh
# No activation needed ever
uv init my-project && cd my-project
uv add requests pandas numpy
uv run python main.py

I spent years typing source .venv/bin/activate because I didn’t know VS Code could do it for me. Now I either select the interpreter once and let VS Code handle activation, or use uv run and forget about virtual environments entirely.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments