Why do we need virtual environment ?

Narendiran Krishnan
4 min readMay 21, 2020

--

The purpose of a Venv is to have a space where we can install packages which are specific to a certain project. For instance, there are lots of projects which are being deployed with Tensorflow 1.x. Version and the current updated version is Tensorflow 2.x. and Let’s consider a scenario with RASA — X framework using a single global environment. Where when we install Rasa -x framework by default it will use the Tensorflow 2.x. Version and other projects which depend on Tensorflow 1.x. version will definitely Importerror as we just updated the Tensorflow library and didn’t find the equivalent code.

So now we understand why we need a separate environment for each project, i.e., each project will be independent of the Module version from each other.

So now let’s look into venv modules and create a Virtual Environment.

In order to do this we need to use Python version 3.3+ and here am using Python 3.7.3 and anything that 3.3+ will do well.

First move to your project directory where we will do all the setups. You really don’t have to install anything for creating a virtual environment, as this comes with the standard library by default. First, you can have a look into my library packages and their version, you can use the following command

either use pip list / pip freeze

if your using ubuntu then do pip3 list / pip3 freeze

cmd: pip list

Well, here you make sure to make a note of the Tensorflow version that I have available in my system, it’s obviously Tensorflow 1.x. and not Tensorflow 2.x.

Now let’s create a virtual environment

python -m “Module Name” “Project / Virtual Environment Name”

python -m venv tf2_version

Here python -m flag which stands for module, i.e. some modules have main entry points and in order to run this has to be used.

This will create your tf2_version virtual environment and you can verify by moving into that directory in your local machine or you can verify by listing it out. In windows use the command “dir” and in Ubuntu use the command “ls”

cmd: python -m venv tf2_version

Now we need to activate the virtual environment and use the following command for it,

“project name”\Scripts\activate.bat

tf2_version\Scripts\activate.bat

Once you activate it, you can confirm it as shown below

cmd: tf2_version\Scripts\activate.bat

You can also verify it with another way by typing “where python”

You can see that the at first the current environment directory is listed.

after activation of venv

Now you do a “pip list” and check for the list of available Modules available by default.

cmd: pip list / pip freeze

Now let’s say our project requires Tensorflow Version 2.x. and so let’s install it.

pip install tensorflow

Here in the comparison image you can verify that from the normal command prompt you can see the Tensorflow Version is 1.x. and in the virtual environment its 2.x.

Tensorflow Version comparision

If in any case you want to get a copy of the list of Modules that’s in this virtual environment you may download as shown below.

pip freeze > requirements.txt

Save your lib / modules to requirements.txt

Now for deactivating our virtual environment, we need to use the command deactivate as shown below

cmd: deavtivate

After this if you wish to delete the virtual environment, it’s just like deleting the folder or from command line you give the following command

rmdir “name of the virtual environment” /s

rmdir tf2_version /s

Here the /s make sure to delete the entire tree and the subtree in it.

cmd: rmdir tf2_version /s

Try it on your own ..!!!

I will let you guys try out this last part on your own.

Now there also another scenario where you like to create a virtual environment with the modules that we have created in the our local machine then you need to use

python -m “Module Name” “Project / Virtual Environment Name” — system-site-packages

python -m venv tf2_version — system-site-packages

If you wish to stay connected,

LinkedIn

Or

you can just google “ narenltk / narendiran krishnan ” or just drop a mail to → narenltk@gmail.com → Happy to help..!!!

--

--

Narendiran Krishnan
Narendiran Krishnan

Written by Narendiran Krishnan

AI blogger. Inspiring & working towards a better future through technology & Artificial Intelligence. Join me in the quest ..!!

No responses yet