How to Auto Activate Virtual Environment in Visual Studio Code on Windows 10: Fix Git Bash 127 Error
Virtual environments are a cornerstone of Python development, allowing you to isolate project dependencies and avoid version conflicts. Visual Studio Code (VS Code) simplifies this workflow by offering built-in support for auto-activating virtual environments. However, Windows 10 users often encounter the frustrating "Git Bash 127 error" when trying to auto-activate their virtual environments. This error (bash: activate: command not found) typically occurs due to misconfigurations between VS Code’s terminal settings, Git Bash, and the virtual environment’s activation scripts.
In this guide, we’ll walk through step-by-step solutions to auto-activate virtual environments in VS Code on Windows 10 and resolve the Git Bash 127 error for good. Whether you’re a beginner or an experienced developer, this blog will demystify the process and ensure your workflow remains seamless.
Table of Contents#
- Understanding the Problem: What Causes the Git Bash 127 Error?
- Prerequisites
- Step-by-Step Guide
- Verify Auto-Activation
- Troubleshooting Common Issues
- Conclusion
- References
Understanding the Problem: What Causes the Git Bash 127 Error?#
The "127 error" in Git Bash indicates a "command not found" issue. When VS Code attempts to auto-activate a virtual environment, it may fail for one of these reasons:
- Mismatched Shell Scripts: Virtual environments on Windows generate activation scripts for different shells:
activate.bat(Command Prompt),Activate.ps1(PowerShell), andactivate(Git Bash/Bash). VS Code may default to the wrong script (e.g.,activate.batfor Git Bash), leading to a "command not found" error. - Incorrect Path Handling: Spaces in project paths or misconfigured working directories can break the path to the
activatescript. - Deprecated VS Code Settings: Older tutorials reference deprecated settings like
terminal.integrated.shell.windows, which no longer work in modern VS Code versions.
Prerequisites#
Before starting, ensure you have the following installed:
- Python 3.6+: Download from python.org. Check "Add Python to PATH" during installation.
- Visual Studio Code: Download from code.visualstudio.com.
- Git Bash: Included with Git for Windows. Select "Use Git and optional Unix tools from the Command Prompt" during installation.
- VS Code Python Extension: Install via the Extensions tab (search for "Python" by Microsoft).
Step-by-Step Guide#
3.1 Create a Virtual Environment#
First, create a virtual environment in your project folder.
-
Open your project folder in VS Code:
- Launch VS Code →
File→Open Folder→ Select your project directory.
- Launch VS Code →
-
Open the integrated terminal:
- Press
Ctrl+` (backtick) or go toView→Terminal.
- Press
-
Create a virtual environment named
venv(replacemy_projectwith your folder name if needed):python -m venv venvThis generates a
venvfolder containing activation scripts.
3.2 Configure VS Code Settings for Auto-Activation#
VS Code’s Python extension can auto-activate virtual environments, but we need to enable this in settings.
-
Open VS Code settings:
- Press
Ctrl+,(comma) or go toFile→Preferences→Settings.
- Press
-
Search for
python.terminal.activateEnvironmentand ensure it’s checked. This tells VS Code to auto-activate the detected virtual environment. -
(Optional) To force VS Code to use your
venv, add the environment path to settings:- In the settings search bar, type
python.defaultInterpreterPath. - Click "Edit in settings.json" and add:
"python.defaultInterpreterPath": "${workspaceFolder}/venv/Scripts/python.exe"
This ensures VS Code uses the
venvPython interpreter. - In the settings search bar, type
3.3 Fix the Git Bash 127 Error#
To resolve the 127 error, we’ll configure VS Code to use Git Bash as the default terminal and explicitly activate the environment with the correct script.
Step 3.3.1 Configure Git Bash as the Default Terminal#
Modern VS Code uses terminal profiles instead of deprecated shell settings. Here’s how to set Git Bash as the default:
-
Open the command palette: Press
Ctrl+Shift+Pand search forTerminal: Select Default Profile. -
Select
Git Bashfrom the list. If it’s missing:- Open
settings.json(PressCtrl+Shift+P→ search forOpen User Settings (JSON)). - Add a Git Bash profile under
terminal.integrated.profiles.windows:"terminal.integrated.profiles.windows": { "Git Bash": { "path": "C:\\Program Files\\Git\\bin\\bash.exe", "args": ["--login", "-i"] // Ensures bash runs in interactive mode } }, "terminal.integrated.defaultProfile.windows": "Git Bash"
Note: Verify the
pathtobash.exe(default:C:\Program Files\Git\bin\bash.exe). - Open
Step 3.3.2 Auto-Run the Activation Command in Git Bash#
VS Code can automatically run the source venv/Scripts/activate command when opening a terminal. Here’s how:
-
In
settings.json, add theterminal.integrated.automationShell.windowssetting to ensure Git Bash runs activation scripts:"terminal.integrated.automationShell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" -
Force the terminal to activate the environment on startup:
- In
settings.json, add:"terminal.integrated.shellArgs.windows": { "Git Bash": ["-c", "source ${workspaceFolder}/venv/Scripts/activate; exec bash"] }
Explanation:
-cruns a command,sourceactivates the environment, andexec bashstarts a new bash session with the activated environment. - In
Verify Auto-Activation#
To confirm the fix works:
-
Close and reopen VS Code (or open a new terminal with `Ctrl+Shift+``).
-
Check the terminal prompt: It should display
(venv)at the start, indicating the virtual environment is active:(venv) user@DESKTOP-XXX MINGW64 ~/my_project -
Validate the Python interpreter path:
which pythonOutput should point to your
venvfolder (e.g.,~/my_project/venv/Scripts/python.exe).
Troubleshooting Common Issues#
Issue 1: (venv) Not Showing Up#
- Check the
activateScript Path: Ensure theactivatescript exists atvenv/Scripts/activate. If missing, recreate the virtual environment withpython -m venv venv. - Quotify Paths with Spaces: If your project path has spaces (e.g.,
My Project), update thesourcecommand insettings.jsonto use quotes:"source \"${workspaceFolder}/venv/Scripts/activate\"; exec bash"
Issue 2: Git Bash Profile Missing in VS Code#
- Reinstall Git Bash: Ensure Git Bash is installed to
C:\Program Files\Git\bin\bash.exe. Reinstall Git for Windows if the path is incorrect.
Issue 3: python.defaultInterpreterPath Not Working#
- Manually Select the Interpreter: Press
Ctrl+Shift+P→ search forPython: Select Interpreter→ Choose./venv/Scripts/python.exe.
Conclusion#
Auto-activating virtual environments in VS Code on Windows 10 streamlines your development workflow, but misconfigurations can lead to the Git Bash 127 error. By creating a virtual environment, configuring VS Code to use Git Bash, and explicitly activating the activate script, you can resolve this error and focus on coding.
Follow the steps above, and let us know in the comments if you encounter other issues!