Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guimondmm/1a2e47d73a191429c7f8b0c12729dc59 to your computer and use it in GitHub Desktop.
Save guimondmm/1a2e47d73a191429c7f8b0c12729dc59 to your computer and use it in GitHub Desktop.
Add an "Anaconda PowerShell Prompt" profile to Windows Terminal. Just paste it in your settings.json file.
{
"colorScheme": "Campbell",
"commandline": "pwsh.exe -ExecutionPolicy ByPass -NoExit -Command \"& '%USERPROFILE%\\anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate '%USERPROFILE%\\anaconda3' \"",
"guid": "{e7d34d61-5a57-4f38-8eab-91cb777b0c26}",
"hidden": false,
"icon": "%USERPROFILE%\\anaconda3\\Menu\\anaconda-navigator.ico",
"name": "Anaconda PowerShell Prompt",
"startingDirectory": "%USERPROFILE%"
}
@guimondmm
Copy link
Author

Description

This Windows Terminal profile launches PowerShell with exactly the same command as Anaconda Navigator does, except it uses the new and improved pwsh.exe (PowerShell Core), instead of the old powershell.exe (Windows PowerShell 5.1), which still ships with Windows as of writing these lines.

That is to say, this profile sources Anaconda's conda-hook.ps1 script, which automatically sets the required environment variables, and activates the base Conda environment. This means you don't need to add Conda to PATH to use your Conda environment in Windows Terminal. It's also more convenient and much faster to launch PowerShell this way than to let conda init wreak havoc on your PowerShell profile.

Just paste this profile at the appropriate location inside your settings.json file, which can be accessed by click the cog in the lower left corner of the Windows Terminal Settings pane.

Caveats

  • As it stands, this profile uses the first pwsh.exe in PATH. If you'd prefer to hardcode the path instead, you can use the (Get-Command 'pwsh.exe').Path cmdlet to get the full path to pwsh.exe.
  • Similarly, this profile assumes your Anaconda install is located at %USERPROFILE%\anaconda3, which is the default folder. Change the path if need be.
  • You don't need to edit your environment variables, and you don't need to add Anaconda to PATH. And you definitely shouldn't let conda init make changes to your Microsoft.Powershell_profile.ps1 file.
  • Don't forget to separate profiles with a comma in your settings.json file. Also, don't forget to escape double quotes and backslashes if you edit the paths to PowerShell or Anaconda.
  • In the extraordinarily unlikely event the GUID above collides with another one of your Windows Terminal profiles, or you don't like the cut of its jib or something, you can generate a new random GUID using the New-Guid cmdlet.

@johnmarquess
Copy link

works like a charm. thanks.

@coolpx
Copy link

coolpx commented Aug 24, 2024

i had to change pwsh.exe to powershell.exe, works great other than that

@atulkumar2
Copy link

atulkumar2 commented Nov 12, 2024

Folks should change the GUID.
Below is the section if using miniconda

{ "commandline": "%PROGRAMFILES%\\PowerShell\\7\\pwsh.exe -ExecutionPolicy ByPass -NoExit -Command \"& '%USERPROFILE%\\miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate '%USERPROFILE%\\miniconda3' \"", "guid": "{ba3f6141-a2f2-5472-b294-a7d7fb0e264f}", "icon": "%USERPROFILE%\\miniconda3\\Menu\\anaconda_powershell_prompt.ico", "name": "miniconda3 powershell 7", "startingDirectory": "%USERPROFILE%" }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment