-
Couldn't load subscription status.
- Fork 1.3k
Fixing "deactivate" command for virtual environments
"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-pythondirectory. -
Edit your
~/.bashrcfile to add the following, you can runcode ~/.bashrcin 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:
Powershell
-
Download the deactivate.ps1 script into say,
~/.vscode-pythondirectory. -
Edit your PowerShell profile to add the following, you can run
code $Profilein 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:
Zsh
-
Download the deactivate script into say,
~/.vscode-pythondirectory. -
Edit your
~/.zshrcfile to add the following, you can runcode ~/.zshrcin 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:
Fish
-
Download the deactivate.fish script into say,
~/.vscode-pythondirectory. -
Edit your
config.fishfile to add the following, you can runcode $__fish_config_dir/config.fishin 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:
Csh
-
Download the deactivate.csh script into say,
~/.vscode-pythondirectory. -
Edit your
~/.cshrcfile to add the following, you can runcode ~/.cshrcin 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:
Other shells
- Use default script for the OS:
- If on Windows, use the script under
Powershellcategory. - Otherwise, use the script under
Bashcategory.
- If on Windows, use the script under
- 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.
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.
Turn off auto-activation: Set "python.terminal.activateEnvironment": false and reopen the shells.