Thursday, February 16, 2017

Journey to Linux(step 1 of n): Virtual Environment

Little about Python versions


Various versions of Python are available and it is one of the most widely used language in open source community. In Linux several system core packages are dependent on python. 

Currently python version 2.7 is widely used and thousands of packages are targeting it in different Linux distributions. If we want to check which python version is installed by default on system.

 

BUT what if we want to use some other python version?

Well its very common scenario that we want to test project with different versions. In order to use multiple python versions without conflicting each other Virtual Environment is the solution.

What is Virtual Environment Anyway?

e.g. Project A has dependency on Python 2.6 and Project B has dependency on 3.0. Both of these project dependencies are other then system default python version i.e. 2.7 here in this case.

What if we change the system default python version to our desired on. Well it is possibly suicidal mission because there are several core Linux packages have dependencies on it. So, answer is simply NO, we will try to blow ourselves doing this usually. Our answer to this problem is virtualenv.
Virtual environment is basically a tool to create an isolated python environment for different projects.
We can have many virtual environments if we have several such dependencies. Each virtualenv will create separate  folder for all required executables to use the desired packages of certain project would need.

How we are going to do this...

It is very easy and i will explain it in few steps below:

Step 1: In order to manage python package we have to install python-pip package manager.


Step 2: Install virtualenv 


 Step 3: Create virtual environment for project 


virtualenv will create a folder in current working directory that will contains pip and python executable files. pip can be use to install other packages.


We can also have python interpreter of out choice like this 


Step 4: Activate the virtualenv


The name of virtual environment will appear on left of prompt now. Now all packages that you install using pip will be placed in projEnvr folder that is quite isolated from global installation.


And when you are done with virtualenv deactivate it


It will put back to system default prompt. And we you want to delete virtualenv then simply delete the folder.