An A — Z Guide On Managing Anaconda Environments Simplified!
Often times Python newbies do not understand the need for these environments. In this article we will be covering the basics of virtual environments in Python.
Why Are Virtual Environments Important In Python?
Simplest to answer that is with an example;
Say you have developed a web application using libraries and frameworks from Python 3,6. Then months later you see there is a new version of Python which is 3.7.
You go ahead and upgrade from 3.6 to 3.7 and your application stops working, once you launch an investigation you notice that by upgrading to 3.7 your application which was build using 3.6 is no longer supported.
By upgrading the to Python 3.7 you also upgraded to 3.7 libraries and you used 3.6 libraries to build your application. Now the question comes up, do you downgrade or upgrade, maybe just downgrade certain libraries and modules to also be 3.6 compatible, but it will be difficult to determine which versions of these libraries and frameworks you used.
Hence virtual environments are important to help isolate projects.
So What Are Virtual Environments?
This of it as a room, whatever you do inside the room does not directly affect anyone outside the room. We can paint the room blue, it does not affect anything that is outside the room. Now lets get started with virtual environments.
Conda Virtual Environments
I prefer creating virtual environments using Conda, I will be showing you how to manage them in Anaconda and using the terminal.
Also referred to as Conda Command prompt, to access it navigate to the search bar and type in Anaconda Prompt, click on it and a Conda terminal will appear.
As you can see on the screen the default virtual environment is base. I have created a few virtual environments for illustration purposes.
To view all our virtual environments we type;
conda env list
From the picture above we have 4virtual environments. The asterisks indicates the virtual environment we are working on currently. Now lets start by creating a virtual environment.
Creating A Virtual Environment
conda create -name pydemo1
conda create -n pydemo1
Both commands do the same thing, the other one name is typed fully and the other we just used (n) to indicate name.
Activating An Environment
Then we simply have to activate our environment using;
conda activate pydemo1
Notice we do not only tell conda to activate but also indicate which virtual environment to activate.
To check if pydemo1 was added to the list of environments and if it is the main environment we use the list command again.
conda env list
And we can notice that pydemo1 is our current virtual environment by the asterisks.
Also notice the locations of the virtual environments are listed. What happens if we want to specify the location for our virtual environment.
Specifying A Location Of An Environment
The location is where our environment lives, so we can determine this by giving conda a the target path.
conda create --prefix ./venv
We have created a new virtual environment and added prefix so we can specify our directory/location.
We can simply activate it and list our environments to check.
Update A Conda Environment
We want to update Python for a particular virtual environment. We are currently sitting at a Python 3.9 version, lets check and make sure.
Yes we are on 3.9.7 and we will type the command quit() to exist out of the additional prompt;
So lets update to Python 3.10 using the command;
conda create -n pydemo2 python=3.10
From the picture above we can see conda proceeded to install Python 3.10 and its default packages. We just need to proceed and check.
First we have to deactivate the current environment which entails our path specification, the perform another deactivation command which involves the pydemo1 environment. The next virtual environment we will meet is base, which is our default virtual environment.
The we can go ahead and activate our virtual environment that is running python 3.10, known as pydemo2
conda activate pydemo2
Then check our python version using the command;
Listing Packages In Your Environment
Lets say we want to check which packages/ libraries I have inside my environment I simply run;
This covers the basics of virtual environments and I will attach links with in-depth information about virtual environments.
If you do prefer to work around virtual environments using the Graphical user interface you can simply open Anaconda Navigator and click on existing environments or create new environments.