Setting up a virtual environment in Anaconda
Why set up a virtual environment ?
Setting up a virtual environment helps isolate different dependencies (external libraries or modules), prevents conflicts and maintain a cleaner project structure.
Let’s put it this way, you’re hosting a party and have decided to cook all the meals in house. You’ve decided on a starter (spring rolls), a main dish (roast chicken) and a dessert (chocolate cake). You set up different prep areas so as to avoid mixing of the ingredients and cross-contamination. That’s exactly what virtual environments serve as, dedicated kitchen spaces that prevent conflicts and isolate different dependencies.
Now that we know why we need virtual environments, we’ll look at the how.
Step 1: Check version and update your Anaconda
conda -V
conda update conda
Step 2: Create virtual environment
conda create -n envname python=version <additional libraries>
envname : the name you want to give to your virtual environment
version: the version of python your codebase requires
additional libraries: any additional packages you would like to install while setting up the environment
Step 3: Activate your environment
Before you can start working in the newly created environment you have to activate it. By default anaconda opens up in the “base” environment.
To activate the environment
conda activate envname
If you have multiple environments, you can list them using
conda info -e
Step 4: Deactivating your environment
To come out of your current environment
conda deactivate
Deactivating is not the same as deleting. Deactivation takes you out of the current environment without deleting or modifying the virtual environment settings.
To delete an environment after you are done with it
conda remove -n envname -all