How not knowing Conda Environments makes You a Rookie

how to conda environment
I'll be honest with you, when I started my journey as a Machine Learning Engineer I never knew how to use a Conda Environment to my advantage, or why I should use it at all! Until I learned about it at work, and now I cannot live without it. Why? For a peaceful life! Trust me when I say this, you too would be relieved to know about what the Conda Environment does, and how you can use it to solve real life problems. So, here's to global peace!

When I started to learn about Conda Environments, I went through a bunch of articles like this one:

Managing environments

...and this one:

In which conda environment is Jupyter executing?

...and this one:

How to Install Jupyter

...and ofcourse, how can I forget this one:

Conda Cheatsheet

But none of these answered my question:

How can I use Conda Environment with Jupyter Notebook, and why should I use it in the first place?

Well, let me answer the why first. As a Machine Learning Engineer, you need to work on multiple projects, most of which will not be using the same packages. For example, you may be developing a Speech Synthesis app for a client based on TenforFlow 2, while for another you might be debugging a PyTorch code. You should not, at any cost, install all the packages at the same place. Instead you need to create separate environments for each project to manage their dependencies and isolate those projects. This ensures that you know the exact packages and dependencies of your code. If you use install all packages at the same place and your code depends on some package which your client does not have installed, you would need to change your code all over again. Your client will not install a new package just because your code depends on it.

Now that we have addressed the why, let's address the how. If you have gone through the links given above, you will see that the answer address the how in fragments, and you never get the complete picture. So I learned it the hard way, just so that you won't have to. Using Conda Environments with Jupyter Notebook involves 5 simple steps. I have listed the steps below with an explanation, the respective codes are given below. All these codes are to be executed in a Conda Shell/Prompt. Also note, the example below shows how to create a Conda Environment and install TensorFlow, then use it in a Jupyter Notebook:

Step 1: Display a list of existing Conda environments to avoid any naming conflicts
Step 2: Create and activate Conda Environment along with TensorFlow 2. I have provided separate codes for both CPU and GPU versions of TensorFlow
Step 3: Activate the Conda Environment you created in Step 2
Step 4: Install Jupyter Notebook in this environment
Step 5: Start Jupyter Notebook from this activated environment

...and here's the code:

Hope this helps. If it does, don't forget to share it on LinkedIn, or your favorite Social Media platform. Remember, sharing is caring.