Skip to content

Fixing "deactivate" command for virtual environments

Kartik Raj edited this page Oct 24, 2023 · 17 revisions

"deactivate" not working by default is a limitation of our new approach to activate terminals. To fix this the following script needs to be added to any of the init scripts for the shell you're using:

Tip: Clicking the "Edit <script>" button on the notification automatically does this editing for you.

Bash


  • Download the deactivate script into say, ~/.vscode-python directory.

  • Edit your ~/.bashrc file to add the following, you can run code ~/.bashrc in bash to open the file in VS Code:

    # >>> VSCode venv deactivate hook >>>
    source <path_to_dir>/deactivate
    # <<< VSCode venv deactivate hook <<<

    where <path_to_dir> is the directory where you downloaded the deactivate script.

  • For example, if path to directory was ~/.vscode-python:

    image


Powershell


  • Download the deactivate.ps1 script into say, ~/.vscode-python directory.

  • Edit your PowerShell profile to add the following, you can run code $Profile in pwsh to open (or if need be, create) the file in VS Code:

    # >>> VSCode venv deactivate hook >>>
    & "<path_to_dir>/deactivate.ps1"
    # <<< VSCode venv deactivate hook <<<

    where <path_to_dir> is the directory where you downloaded the deactivate script.

  • For example, if path to directory was ~/.vscode-python:

    image


Zsh


  • Download the deactivate script into say, ~/.vscode-python directory.

  • Edit your ~/.zshrc file to add the following, you can run code ~/.zshrc in zsh to open the file in VS Code:

    # >>> VSCode venv deactivate hook >>>
    source <path_to_dir>/deactivate
    # <<< VSCode venv deactivate hook <<<

    where <path_to_dir> is the directory where you downloaded the deactivate script.

  • For example, if path to directory was ~/.vscode-python:

    image


Fish


  • Download the deactivate.fish script into say, ~/.vscode-python directory.

  • Edit your config.fish file to add the following, you can run code $__fish_config_dir/config.fish in fish to open the file in VS Code:

    # >>> VSCode venv deactivate hook >>>
    source <path_to_dir>/deactivate.fish
    # <<< VSCode venv deactivate hook <<<

    where <path_to_dir> is the directory where you downloaded the deactivate script.

  • For example, if path to directory was ~/.vscode-python:

    image


Csh


  • Download the deactivate.csh script into say, ~/.vscode-python directory.

  • Edit your ~/.cshrc file to add the following, you can run code ~/.cshrc in csh to open the file in VS Code:

    # >>> VSCode venv deactivate hook >>>
    source <path_to_dir>/deactivate.csh
    # <<< VSCode venv deactivate hook <<<

    where <path_to_dir> is the directory where you downloaded the deactivate script.

  • For example, if path to directory was ~/.vscode-python:

    image


Other shells


  • Use default script for the OS:
    • If on Windows, use the script under Powershell category.
    • Otherwise, use the script under Bash category.
  • Add the script at the end of corresponding initialization script for your shell.

Make sure to restart the shell after editing your script, reloading VS Code will not work to apply this change.

Why it works:

In the new approach, we do not run <venv>/<bin>/activate script which traditionally registers the deactivate shell hook. Hence we need to add it manually to shell initialization script (~/.bashrc for example) which is executed automatically when a terminal starts.

Alternative solution:

Turn off auto-activation: Set "python.terminal.activateEnvironment": false and reopen the shells.

Clone this wiki locally