Windows Terminal conda
Tags: windowsterminal, conda, beginners
Published: 2020-05-26 00:27:15

## What's the problem?
If you code in Python you've probably encountered the problem of different dependencies required in different projects. Not to search very far, some projects still require Python 2.X to run, while the other work only with Python 3.X.

Probably on of the first solutions which comes to mind is having some sort of 'environments' for different modules/interpreters. With such an approach you can just switch between different 'environments' when you need it. For example you can have an "Convolutional Neural Networks Environment" with a Tensorflow + OpenCV and "Data Analysis Environment" with Pandas + Matplotlib. Then you can just switch/activate one of them when you need it. Sounds nice, isn't it?

## conda to the rescue!
Fortunately, you don't have to create some fancy bash/PowerShell scripts by hand to do this. Creators of [conda](https://docs.conda.io/) have already solved the problem! It enables you to do the exact things described above - create individual, isolated environments with different packages or even whole different Python interpreters. What makes it even better is its popularity and community support. 

> ### [conda vs Miniconda vs Anaconda](https://stackoverflow.com/a/45421527/9375075)
> When you'll start looking for some "conda installation guide" you'll immediately face the problem of those 3... things above. What is it all about? To keep it short:
> - **conda** is just a package management system. Something like `apt-get`, `chocolatey` or brand new [winget](https://docs.microsoft.com/en-us/windows/package-manager/winget/). It facilitates installation & management of different packages
> - **miniconda** is already pre-built and pre-configured collection of the packages. To make it as small as possible, it contains only `conda` + its dependencies. If you want to have other packages, you'll need to download them individually with `conda`
> - **anaconda** is similar to miniconda, but the collection of the pre-installed packages is much bigger. You can still download some packages using `conda`, but probably you won't have to if you are just starting and using some common ones.

Here's a super-short miniconda quickstart
1. Download proper [installer](https://docs.conda.io/en/latest/miniconda.html) and install miniconda (when in doubt, choose the recommended options).
2. Run _Anaconda Prompt_ from Start menu. A terminal window should appear with something like `(base) D:\Desktop>`. That `(base)` prefix indicates that you're currently using _base_ (default) conda environment.
3. To check, what packages are installed in that environment: `conda list`
4. To create a new "superNewEnv" environment: `conda create --name superNewEnv` 
5. To install SciPy in this environement: `conda install --name superNewEnv scipy`
6. To activate an environment: `conda activate superNewEnv`. You can always see your active environment at the beginning of the prompt (like that `base` at the beginning).
7. Now, if you run Python inside the `superNewEnv` environment you'll be able to use SciPy. What's even better, it can be used _only_ there, the `base` environment is not cluttered.
8. Check out the [docs](https://docs.conda.io/projects/conda/en/latest/user-guide/index.html) or [cheatsheet](https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf) to dive deeper.

## But I want my terminal!
The minor issue with conda is its interoperability with your typical command line or PowerShell. `conda` commands won't work there, only in its special prompt. However, it can be partially solved with the new [Windows Terminal](https://github.com/microsoft/terminal). I won't describe all its features here, but you should definitely check it out. The thing which is interesting for us right now is its concept of _profiles_. In short, they are preconfigured different ways of launching command line, terminal or whatever you call it.

After installing and launching the terminal, go to its settings by clicking an arrow on the tab bar:
![GIF showing location of the settings](https://dev-to-uploads.s3.amazonaws.com/i/tf28h9uoe9xvfbjgvfub.GIF)
The settings don't have any GUI yet, it's just a JSON file you can modify. Therefore they'll open in some default JSON editor (VS Code in my case).

In the file look for `profiles` property. It has a `list` of different profiles created by Windows Terminal automatically. We'll just one more to that list.

### What's special about miniconda prompt?
If you examine a bit the thing you're launching from the start menu, you'll discover that the shortcut just runs a cmd/PowerShell with some arguments:

`%windir%\System32\cmd.exe "/K" C:\Users\%USERNAME%\miniconda3\Scripts\activate.bat C:\Users\%USERNAME%\miniconda3`

(to find it out yourself, search for Anaconda prompt, click it with right mouse button, "Open File location" and check the properties of the shortcut).

Now let's have that prompt in our Windows Terminal! It boils down to adding another profile which will launch the cmd/PowerShell as shown above. Just add another element to the `list` in `profiles`:
```json
{
    "guid": "{2337f50b-bd5c-4877-b127-d643395b8fe2}",
    "hidden": false,
    "name": "Miniconda",
    "commandline": "powershell.exe -ExecutionPolicy ByPass -NoExit -Command \"& 'C:\\Users\\%USERNAME%\\miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\%USERNAME%\\miniconda3' \""
}
```
There are 3 things wort mentioning here:
1. Value of GUID does not matter. Just make sure it's unique among all the profiles
2. Value for `commandline` differs a bit from the one above, as I decided to use PowerShell instead of cmd
3. Watch out for backslashes and escape all of them + quotemarks properly!

## Final touches
The new profile "Miniconda" should be available in the terminal dropdown. Just to make it a bit prettier
1. Let's add the conda icon! 

    First find some version of it you like (ex. [that one](https://www.psych.mcgill.ca/labs/mogillab/anaconda2/lib/python2.7/site-packages/anaconda_navigator/static/images/anaconda-icon-1024x1024.png)). Save it somewhere on your machine and add `icon` field to the Miniconda profile:
`"icon": "path\\to\\your\\icon.png"`
2. Always display "Miniconda" in the tab name. 

    Add `"suppressApplicationTitle": true` to the profile.

Final result:
![GIF with final result](https://dev-to-uploads.s3.amazonaws.com/i/41zfomd35alznp9mj5t0.GIF)
Final profile settings:
```json
{
	"guid": "{2337f50b-bd5c-4877-b127-d643395b8fe2}",
	"hidden": false,
	"name": "Miniconda",
	"suppressApplicationTitle": true,
	"commandline": "powershell.exe -ExecutionPolicy ByPass -NoExit -Command \"& 'C:\\Users\\%USERNAME%\\miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\%USERNAME%\\miniconda3' \"",
	"icon": "%OneDrive%\\Pictures\\Saved Pictures\\conda.png"
},
```


<br><br>
_PS.
To make your terminal look prettier check out that [blog post by Scott Hanselman](https://www.hanselman.com/blog/HowToMakeAPrettyPromptInWindowsTerminalWithPowerlineNerdFontsCascadiaCodeWSLAndOhmyposh.aspx)_