Puget Systems print logo
https://www.pugetsystems.com
Read this article at https://www.pugetsystems.com/guides/1907
Dr Donald Kinghorn (Scientific Computing Advisor )

Note: Adding Anaconda PowerShell to Windows Terminal

Written on October 1, 2020 by Dr Donald Kinghorn
Share:

Introduction

In my opinion Windows Terminal (wt) is one of the best applications Microsoft has added to Windows 10 (ever). It is a nice terminal app similar to some of the good Linux terminal. It is a modern tabbed, customizable terminal. On my typical daily Windows desktop I will have wt open with several tabs, including; PowerShell, a couple of WSL2 Linux shells and an SSH remote session or two.

When running Python on Windows I use conda with either a Miniconda or full Anaconda install. And, I use it from PowerShell. Running conda from PowerShell is almost identical to running from bash on Linux. This helps to keep workflow consistency.

When you install Miniconda3 or Anaconda3 on Windows it adds a PowerShell shortcut that has the necessary environment setup and initialization for conda. It's listed in the Windows menu as "Anaconda Powershell Prompt (Anaconda3)".

Examining the properties of this shortcut for the system I'm using right now shows,

%windir%System32WindowsPowerShellv1.0powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:UsersdonAnaconda3shellcondabinconda-hook.ps1' ; conda activate 'C:UsersdonAnaconda3' "

(This is reflective of my user name and Anaconda install location on this particular machine.)

That's an easy way to start up your Python "Base" conda environment. However, this opens a separate/detached PowerShell instance and it would be nice to have this as an optional shell from Windows Terminal!

What we will do in this post is add the functionality of the above start-up commands as a new shell option in Windows Terminal.

Adding a new custom shell to Windows Terminal

Note: I am using "Windows Terminal Preview Version: 1.4.2652.0". If you are using an older version of Windows Terminal your setting.json file may be different than what I show below! ( If you are not using "Preview" I recommend that you go ahead and install it, it's quite nice! )

From Windows Terminal go to the settings option on the drop-down menu,

Clicking that will open the wt settings.json file in vscode (you are using vscode aren't you?!)

We will add a new profile to the end of the profiles list in this setting.json file.

Copy the profile entry for the default PowerShell terminal entry (should be the first entry). Past this to the end of the profiles. Don't forget to add a comma after what was previously last profile.

,
{
    // Make changes here to the powershell.exe profile.
    "colorScheme": "Campbell Powershell",
    "guid": "{}",
    "name": "Windows PowerShell",
    "commandline": "powershell.exe",
    "hidden": false
}

I have removed the "guid" that was there since we will need to generate a new unique one for this profile.

Generate a new "guid"

You will need a new unique guid for your terminal profile. In PowerShell run "New-Guid",

PS C:> New-Guid
Guid
----
0352cf0f-2e7a-4aee-801d-7f27f8344c77

Don't use the one listed above. Make a new one!

Copy your new guid to between the curly braces in the profile setting. For example,

"guid": "{0352cf0f-2e7a-4aee-801d-7f27f8344c77}",

Complete the profile configuration

The finial profile configuration I settled on looks like,

{
    // custom PowerShell profile for conda.
    "colorScheme": "One Half Dark",
    "guid": "{0352cf0f-2e7a-4aee-801d-7f27f8344c77}",
    "name": "conda PowerShell",
    "commandline": "pwsh.exe -ExecutionPolicy ByPass -NoExit -Command "& '~/Anaconda3/shell/condabin/conda-hook.ps1' ; conda activate '~/Anaconda3'" ",
    "icon": "C:/Users/don/Documents/Icons/python.png",
    "startingDirectory": "~",
    "hidden": false
}

Notes:

  • I used my favorite "One Half Dark" for the color scheme. Here is a link for a list of built in themes.
  • Remember to use the "New-Guid" command to generate a fresh guid for your configuration.
  • I used pwsh.exe for the command line because I am using PowerShell core v7. The default installed PowerShell is v5.1. You can use powershell.exe in the profile instead of pwsh.exe if you are using the default PowerShell.
  • linked to a Python icon downloaded from the net. You could use most any image you like to add a nice touch to your configuration.
  • The starting directory is set to your user home directory, "~" is a shorthand for that.
  • Documentation for customizing Windows Terminal is here https://docs.microsoft.com/en-us/windows/terminal/customize-settings/profile-settings

Win Terminal with conda shell

If you are curious about how I got those customized WSL2 Ubuntu shells in there then have a look at,

Note: How To Copy and Rename a Microsoft WSL Linux Distribution

Hope this is helpful for you!

Happy computing! --dbk @dbkinghorn -- Best wishes and stay safe!


Looking for a GPU Accelerated Workstation?

Puget Systems offers a range of poweful and reliable systems that are tailor-made for your unique workflow.

Configure a System!

Labs Consultation Service

Our Labs team is available to provide in-depth hardware recommendations based on your workflow.

Find Out More!

Why Choose Puget Systems?


Built specifically for you

Rather than getting a generic workstation, our systems are designed around your unique workflow and are optimized for the work you do every day.

Fast Build Times

By keeping inventory of our most popular parts, and maintaining a short supply line to parts we need, we are able to offer an industry leading ship time.

We're Here, Give Us a Call!

We make sure our representatives are as accessible as possible, by phone and email. At Puget Systems, you can actually talk to a real person!

Lifetime Support/Labor Warranty

Even when your parts warranty expires, we continue to answer your questions and even fix your computer with no labor costs.

Click here for even more reasons!

Puget Systems Hardware Partners

Tags: Notes, Windows, PowerShell, Windows Terminal
Donald Kinghorn

I just did this again and I had to remove one layer of " " to get it to work this time???

went from

"commandline": "pwsh.exe -ExecutionPolicy ByPass -NoExit -Command "& '~/Anaconda3/shell/condabin/conda-hook.ps1' ; conda activate '~/Anaconda3'" ",

"commandline": "pwsh.exe -ExecutionPolicy ByPass -NoExit -Command & '~/Anaconda3/shell/condabin/conda-hook.ps1' ; conda activate '~/Anaconda3' ",

Posted on 2021-03-13 02:53:22
Hypersphere

Donald,

Many thanks for this. I can't believe it took me this long to discover Windows Terminal, but I found it today. I had seen various posts on "Windows Terminal" and mistakenly thought they were referring to the Command Prompt!

I found your article while searching for ways to access the Anaconda prompt in the Windows command prompt.

In my Linux installation of Anaconda, I have things configured so that the terminal prompt shows "(base)", thereby indicating that the base python environment has been activated. However, I cannot get this to show up in the Windows command prompt in my Windows installation of Anaconda. I have added the appropriate lines to my .condarc file, but these have no effect on the command prompt. Is there a way to do this?

However, I have noticed that the Windows Terminal Powershell prompt shows the "(base)" indicator by default, so I am thinking of simply using Windows Terminal Powershell as my default terminal in Windows. What would be the advantages or disadvantages of doing this rather than switching among Powershell, Command Prompt, Anaconda Powershell, or Anaconda Prompt?

Posted on 2021-07-01 15:32:30
Donald Kinghorn

Yes, Windows Terminal App is one of the best things MS has done :-) I spent last year on Win 10 using the teminal, WSL2 and vscode. I could do everything I needed. I only created files in WSL2 in order to keep them being clobbered by NTFS :-) It worked well. I'm back on a Linux desktop now for most of my work but I can go back and forth pretty easily now.

I just checked this on my Win10 laptop. It was throwing me an error from using "~" as the start directory. I had to change it to this line

From
"startingDirectory": "~",
To
"startingDirectory": "%userprofile%",

Then it worked as above. Note that I have PowerShell 7 installed thus the "pwsh" instead of "powershell".

In the terminal setting for the Anaconda prompt the part that is setting the prompt (base) is from this,
'~/Anaconda3/shell/condabin/conda-hook.ps1'

It's that conda-hook.ps1 that has a command at the bottom (that is defined near the end of Conda.psm1)
Add-CondaEnvironmentToPrompt

I'm not sure exactly what is going on with your setup and I'm not sure where conda puts it's ".condarc" file on Windows ?? The PowerShell thing I did in this post should just startup the PS shell with whatever you have set from the Windows Anaconda3 install.

The default behavior for conda is to have (base) activate on shell startup and to show the prompt. On Linux I disable that in my .condarc file,

kinghorn@i9:~$ cat .condarc
auto_activate_base: false

Hope all of this is helpful! :-) take care my friend --Don

Posted on 2021-07-01 19:29:05
Hypersphere

Hi Don,

While trying to use your original post to create a "Conda PowerShell" as a new shell in Windows Terminal, I kept getting json errors. Here is the configuration that finally worked for me:

},
{
"colorScheme": "RJR_Green1",
"commandline": "powershell.exe -ExecutionPolicy ByPass -NoExit -Command ~/anaconda3/shell/condabin/conda-hook.ps1 ; conda activate ~/anaconda3",
"fontFace": "Consolas",
"fontSize": 14,
"guid": "{23951d87-5d2a-4678-bff4-4fbd17112f68}",
"hidden": false,
"name": "Conda PowerShell",
"startingDirectory": "c:/home/hypersphere",
"icon": "c:/Users/hypersphere/Pictures/icons/python_logo_crop.png"
},

As you can see, for the startingDirectory and icon entries, I used the full path to my home directory, but not so in the commandline.

However, in the commandline, I found that I needed to remove single or double quotes except for the double quotes enclosing the entire line. I also removed the ampersand (&).

In addition, I discovered to my delight that forward slashes can be used, just as in linux. Backslashes could be used as well, but they need to be escaped, which can get confusing.

Posted on 2021-07-01 20:18:15
Donald Kinghorn

Great! The config for this seems to be "dynamic" :-) I've had to make a few adjustments since the first time I tried. Not too surprising since this is still under active development.

Yes, I really like using powershell with the more linux/unix like command syntax. Quite a few commands just work at least in their basic form.

I feel that Windows is completely usable :-) And I'm looking forward to WSL with GPU compute support coming out in the general release. I guess that will be Windows 11.

Posted on 2021-07-01 23:29:12