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:
source .venv/bin/activateI 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:
pip install requests pandas numpyThen later I ran my script in another terminal where the venv was active:
$ python main.pyTraceback (most recent call last): File "main.py", line 1, in <module> import requestsModuleNotFoundError: No module named 'requests'Confused, I checked my installed packages:
$ 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.
┌─────────────────────────────────────────────────────────────────┐│ 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
# Create venv in project directorypython -m venv .venv
# Or using uv (faster)uv venvI 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:
Python: Select InterpreterVS Code showed me a list of detected Python interpreters:
> 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:
python.terminal.activateEnvironmentThe 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:
(.venv) user@machine:~/project$The (.venv) prefix appeared automatically. No manual activation needed.
Let me verify:
$ which python/home/user/project/.venv/bin/python
$ pip listPackage Version---------- -------pip 24.0My virtual environment was active from the moment the terminal opened.
How It Works
Here’s what happens behind the scenes:
┌─────────────────────────────────────────────────────────────────┐│ 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:
{ "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:
{ "python.defaultInterpreterPath": "${workspaceFolder}/.venv/Scripts/python.exe"}Alternative: The uv Approach
Remember the Reddit suggestion about uv run? Let me try that approach too.
# Install uvcurl -LsSf https://astral.sh/uv/install.sh | sh
# Create a projectuv init my-projectcd my-project
# Add dependenciesuv add requests pandas numpy
# Run any Python command - no activation neededuv run python main.pyThe uv run command handles the virtual environment internally. I never think about activation at all.
# 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 environmentThis 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:
-
I created venvs but never selected interpreters: VS Code can’t auto-activate what it doesn’t know about.
-
I assumed it was complicated: The solution is literally one Command Palette action.
-
I got used to the manual workflow: After typing
source .venv/bin/activatehundreds of times, it became muscle memory. I stopped questioning it. -
The setting is hidden:
python.terminal.activateEnvironmentworks silently in the background. There’s no UI indicator that auto-activation is happening.
Common Mistakes
Mistake 1: Creating venv Outside the Workspace
# WRONG - venv outside project folder$ cd ~/virtualenvs$ python -m venv myproject-env
# VS Code can't find it automaticallyThe venv should be inside the project:
# CORRECT - venv inside project$ cd ~/projects/myproject$ python -m venv .venvMistake 2: Using Different venv Names
# Inconsistent names across projectsproject-a/├── venv/ # One project uses 'venv'
project-b/├── env/ # Another uses 'env'
project-c/├── .venv/ # Another uses '.venv'This causes confusion. Stick with .venv:
# CONSISTENT - always use .venvproject-a/├── .venv/
project-b/├── .venv/
project-c/├── .venv/Mistake 3: Not Restarting VS Code After Creating venv
# Create venv$ python -m venv .venv
# Immediately try to select interpreter in VS Code# → .venv might not appear in list yetAfter 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:
{ "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:
# WRONG on Windowssource .venv/bin/activate # This is Unix path
# CORRECT on Windows.venv\Scripts\activateVS 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:
# 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 activateEnvironmentpython.terminal.activateEnvironment: true
# Check 3: Does the venv exist?$ ls .venv/bin/activate.venv/bin/activate # Should existMultiple venvs Confusion
If VS Code selects the wrong venv:
# List all detected interpreters# Cmd+Shift+P → "Python: Select Interpreter"
# Or specify in settings.jsonecho '{"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python"}' > .vscode/settings.jsonTerminal Shows Different Python
$ which python/usr/bin/python # Wrong! System Python
# Fix: Manually select interpreter in VS Code# Cmd+Shift+P → "Python: Select Interpreter" → Choose .venvTime Savings
Let me calculate what auto-activation saves:
Manual activation per terminal: 5 secondsTerminals opened per day: 15Time saved per day: 75 secondsWorking days per year: 250Time saved per year: 5.2 hours
Plus: Zero "installed to wrong environment" mistakesPlus: Zero "why can't I import my package" debugging sessions5 hours per year saved, plus fewer mistakes. All from one configuration step.
Which Approach Should You Use?
┌─────────────────────────────────────────────────────────────────┐│ 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:
# 1. Create venvpython -m venv .venv
# 2. In VS Code: Cmd+Shift+P → "Python: Select Interpreter"# Choose .venv/bin/python
# 3. Open new terminal# Prompt shows: (.venv) automaticallyOr for the modern approach with uv:
# No activation needed everuv init my-project && cd my-projectuv add requests pandas numpyuv run python main.pyI 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:
- 👨💻 VS Code Python Extension Settings
- 👨💻 Python Extension for VS Code
- 👨💻 uv - Modern Python Package Manager
- 👨💻 Reddit: VS Code Python Terminal Activation
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments