DEV Community

Piotr Ładoński
Piotr Ładoński

Posted on

Windows Terminal conda

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 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

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. 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 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 or cheatsheet 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. 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
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:

    "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' \""
Enter fullscreen mode Exit fullscreen mode

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). 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
Final profile settings:

    "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"
Enter fullscreen mode Exit fullscreen mode

To make your terminal look prettier check out that blog post by Scott Hanselman

Top comments (1)

winand profile image
Makarov Andrey

"startingDirectory" is another useful option