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#

  1. Understanding the Problem: What Causes the Git Bash 127 Error?
  2. Prerequisites
  3. Step-by-Step Guide
  4. Verify Auto-Activation
  5. Troubleshooting Common Issues
  6. Conclusion
  7. 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), and activate (Git Bash/Bash). VS Code may default to the wrong script (e.g., activate.bat for 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 activate script.
  • 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.

  1. Open your project folder in VS Code:

    • Launch VS Code → FileOpen Folder → Select your project directory.
  2. Open the integrated terminal:

    • Press Ctrl+` (backtick) or go to ViewTerminal.
  3. Create a virtual environment named venv (replace my_project with your folder name if needed):

    python -m venv venv

    This generates a venv folder 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.

  1. Open VS Code settings:

    • Press Ctrl+, (comma) or go to FilePreferencesSettings.
  2. Search for python.terminal.activateEnvironment and ensure it’s checked. This tells VS Code to auto-activate the detected virtual environment.

  3. (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 venv Python interpreter.

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:

  1. Open the command palette: Press Ctrl+Shift+P and search for Terminal: Select Default Profile.

  2. Select Git Bash from the list. If it’s missing:

    • Open settings.json (Press Ctrl+Shift+P → search for Open 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 path to bash.exe (default: C:\Program Files\Git\bin\bash.exe).

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:

  1. In settings.json, add the terminal.integrated.automationShell.windows setting to ensure Git Bash runs activation scripts:

    "terminal.integrated.automationShell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
  2. 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: -c runs a command, source activates the environment, and exec bash starts a new bash session with the activated environment.

Verify Auto-Activation#

To confirm the fix works:

  1. Close and reopen VS Code (or open a new terminal with `Ctrl+Shift+``).

  2. Check the terminal prompt: It should display (venv) at the start, indicating the virtual environment is active:

    (venv) user@DESKTOP-XXX MINGW64 ~/my_project
  3. Validate the Python interpreter path:

    which python

    Output should point to your venv folder (e.g., ~/my_project/venv/Scripts/python.exe).

Troubleshooting Common Issues#

Issue 1: (venv) Not Showing Up#

  • Check the activate Script Path: Ensure the activate script exists at venv/Scripts/activate. If missing, recreate the virtual environment with python -m venv venv.
  • Quotify Paths with Spaces: If your project path has spaces (e.g., My Project), update the source command in settings.json to 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 for Python: 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!

References#