What is a virtual Environment

What is a virtual Environment

ยท

5 min read

Do you ask yourself what is a virtual environment and why are all developers use it all the time? Well, that is why I am here to clear all the skepticism.

What is a Virtual Environment?

A virtual environment is a tool that helps keep the required dependencies of different projects by isolating them in a python virtual environment. To explain further in lame terms it creates a space in your machine where all your downloaded packages will stay instead of it installing on the entire computer system.

Where can we use a virtual environment?

Let us imagine a scenario where you want to create a project that requires Django version 3.1 and you run on Django 1.7. In such a case having a virtual environment will help you to run both python dependencies on your system with ease.

Now to solve the problem above we just need to create a virtual environment that will hold Django version 3.7 and all other dependencies required for that specific project. The great thing about this is that there are no limits to creating a virtual environment since they are just like directories containing scripts.

How does a virtual environment work?

I have been talking about the sweets ๐Ÿฌ and good of a virtual environment now let's see how they work and how to install them.

we use a python module named virtualenv. And this will help create an isolated python environment that contains all necessary executables packages that a python project will need.

To use Virtualenv we have to install it

$ pip install virtualenv

The installation shouldn't take more than 1 minute depending on your internet speed.

Let us confirm the installation

$ virtualenv --version

if that ran successfully you should see:

virtualenv 20.2.1

And that is a confirmation that you have virtualenv installed on your machine

Using VirtualEnv

You can create a virtual environment using the following command:

$ virtualenv V_name

V_name in that instance is going to be the name of my virtual environment, once the code above typed and the enter button has been clicked you will have a wait time of nothing less than 20 sec and boom your virtual environment has been created ๐ŸŽ‰. Now your virtual environment has been created you can start downloading all the dependencies/packages you need right? hmmm nope not yet ( drinks coffee โ˜•๐Ÿ˜ ). We need to activate our virtual environment.

Activate VirtualEnvironment

$ source virtualenv_name/bin/activate

So in my case, it would be

$ source V_name/bin/activate

Once the virtual environment is activated, the name of your virtual environment will appear on the left side of the terminal. This will let you know that the virtual environment is currently active.

(V_name)$

Now you can install you dependencies

(V_name)$ pip install django -- version 3.1

Now Django version 3.1 is installed in my virtualenv named V_name. It doesn't hurt to confirm what packages you have installed on your virtual environment you know ๐Ÿ˜.

Type:

pip list

And this โฌ†๏ธ will show you all the packages and version you have currently in your virtual environment โฌ‡๏ธ

Package                                Version
--------------------------------------
Django                                     3.1.0

Once you are done with the work, you can deactivate the virtual environment by the following command:

(virtualenv_name)$ deactivate

Now you will be back to the systemโ€™s default Python installation.

Conclusion

In my opinion, a virtual environment should be used whenever you work on any python based project. It is overall good to have one virtual environment for every python-based project you work on so that each dependency of every project is isolated from the system and each other.