Virtual Environments: Python & Anaconda
A virtual environment is an isolated environment that helps to keep dependencies required by different projects separate. As a python developer virtual environments are very important.
Why do we use virtual environments
By default, every project on your system will use the same directories to store and retrieve site-packages (third party libraries). For instance, you are working on two web-based python projects and one of them uses Django 1.11 and the other uses Django 3.1. This is a real problem for Python since it can’t differentiate between versions in the “site-packages” directory. In such situations, the virtual environment can be really useful to maintain the dependencies of both projects.
The challenge above can be resolved by creating two separate virtual environments for both projects. The great thing about this is that there are no limits to the number of environments you can create.
In this article, we'll create two different python environments, a python (pip) environment and a conda environment. Fundamentally, the difference between both is as follows. Pip is the python package manager for python while conda is a package manager for python, but it can package and distribute software for any language. Here's a link that goes into detail on the many differences between Conda and Python (Pip)Environment
Create a Pip python Environment
Launch the command line terminal, navigate to the directory where you would like to create your virtual environment in and check that the system is updated with the command below:
~$ sudo apt-get update
Then the command is used to install the virtual environment:
~$ python3 -m pip install --user virtualenv
I used the following command to first create your virtual environment where “virt_env_name” is the name of your virtual environment:
python3 -m venv virt_env_name
Then activate the virtual environment that was just created.
There are two ways to install packages required for a project. The First would be to simply install the package with its name. For example:
~$ python3 -m pip install pandas
On the other hand, some projects come with a text file of required packages. In this case, we would add an option "-r " to read the text file and ensure the text file is within our current working directory and run the command like below.
~$ python3 -m pip install -r requirements.txt
To Deactivate your environment, use the command deactivate or Ctrl+D
Create a Conda environment
Launch the command line terminal or the Anaconda Prompt if Anaconda is already installed, then follow the steps below:
To create an environment:
conda create --name virt_env_name
Similarly, replace virt_env_name with your chosen environment name.
When conda asks you to proceed, select y for "yes":
This creates the virt_env_name environment in /envs/. No packages will be installed in this environment.
conda activate virt_env_name
To install packages with conda, the command below would be used. For instance, if you want to install pandas;
conda install pandas
In the case you want to go to another environment, the command below deactivates the current environment and takes you back to the base environment.