Skip to content

Fixing "deactivate" command for virtual environments

Kartik Raj edited this page Oct 18, 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 ~/.vscode-python/deactivate
    # <<< VSCode venv deactivate hook <<<
  • For example: image

Powershell


  • Download the deactivate.ps1 script into say, ~/.vscode-python directory.
  • Edit your 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 >>>
    & "~/.vscode-python/deactivate.ps1"
    # <<< VSCode venv deactivate hook <<<
  • For example: 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 bash to open the file in VS Code:
    # >>> VSCode venv deactivate hook >>>
    source ~/.vscode-python/deactivate
    # <<< VSCode venv deactivate hook <<<
  • For example: image

Fish


  • Download the deactivate.fish 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 ~/.vscode-python/deactivate.fish
    # <<< VSCode venv deactivate hook <<<
  • For example: 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 bash to open the file in VS Code:
    # >>> VSCode venv deactivate hook >>>
    source ~/.vscode-python/deactivate
    # <<< VSCode venv deactivate hook <<<
  • For example: 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