Introduction to virtual environment


What is a virtualenvironment ?

A  virtualenvironment is basically an isolated environment that we create in our system to have separate work environment for all the concurrent projects that we work on or let's say plan to work on.

Details on virtualenvironment and packages can be found in their official documentation : here

Why do we need a virtualenvironment ?

  • As creating a virtualenvironment gives you complete isolation for your packages and installation for that specific project. Sometimes we need that for working on different projects concurrently where each project has it's own requirements and package dependencies. For example, If you are working in two different projects like core-python web-framework and a machine learning project. Then those two projects will ideally have a number of different package requirements except for a number of generic packages. So instead of installing everything in a single place(globally), you should make separate isolated environments for them and install the dependencies there.

  • If you have two different projects A and B which depend on package C but different versions. Then there will be a conflict if A and B depend on different versions of C (let's say C.0.1 and C.0.2). The global site package will contain either C.0.1 or C.0.2 in the C package. So, it's better to have two separate virtualenvironments for A and B having C.0.1 and C.0.2 installed in them. A more detailed explanation: here.

How to install and setup a virtualenvironment ? 

The "pip" in python has made the job easier to download and install a virtualenvironment in your local system.
if you don't have pip in your system. You can get info on how to get pip from here.

Open a command prompt and run the following to get it using pip:
>> pip install virtualenv

Now create a project directory and cd to that directory.
>> mkdir test
>> cd test

To create a virtual environment. Type in the following command in cmd.
>> virtualenv venv


The command above will create a directory inside test named venv which will contain the virtual environment.
The content of the folder(venv) looks like this:


To activate the virtual environment type in and run the following command in cmd.
(The backslashes are important):
>>venv\scripts\activate.bat


Once activated(the (venv) gets prefixed to the command prompt), anything you try to execute gets executed inside the virtual environment, IN ISOLATION.

Let's verify,

I have flask installed globally in my system. Now, let's find out what happens if I try to import flask from inside venv.


As expected, venv being an isolated environment does not have access to the global packages and tries to find flask in the venv/lib/site-packages and flask is not there.

Now, let's install flask and then try to import it again.


And it works!

You can always check from where your python executable is being fetched from inside your code. Using the sys library. 


It shows the python.exe is being fetched from venv/scripts. Deactivating the virtualenvironment and running this command again will clearly display a different path (the global python installation) to the python.exe.


What is virtualenvwrapper ?

Instead of  doing venv\scripts\activate.bat every time you could just type "workon venv" and achieve the same.

creating a virtualenv and activating it at the same time can also be achieved by simply using "mkvirtualenv <environmemnt_name>"

virtualenvwrapper is a wrapper on top of virtualenv. It makes using virtualenv a bit easier with all it's extensions and wrapper modules. It's easy to get (remember, pip!!)

Install it using pip like below:
>> pip install virtuaenvwrapper

More on virtualenvwrapper can be found in the documentation : here




I hope that helps to start with virtualenv !!


Comments