Translate in Your language

Wednesday, 19 February 2020

virtualenv tool in Python for creating virtual environments.

virtualenv is a tool that allows you to create a virtual environment in python. In other words, it is a tool to create isolated Python environments, each of which is a folder that contains all the necessary executables to use the packages that a Python project would require. Let's try to understand this with an example.

Let's assume that we install Python on our system and we start working on a website for Client X. We create a project folder called ProjectX and start coding for the website. Also install some libraries; for example, the Django framework. Lets assume the Django version we install for ProjectX is 1.7.

Now, let's assume that we get another client Y. Now the Client Y wants us to build another website for them, so we start ProjectY and, along the way, we need to install Django again. However, this time issue is that now the Django version required is 1.8 for ProjectY and we cannot install it on our system because this would replace the version we installed for ProjectX and result in risk of introducing incompatibility issues.

So to handle this situation, we will use virtualenv tool. We will create a virtual environment for ProjectX, install all the dependencies, and then we will create a virtual environment for ProjectY, installing all its dependencies without hesitation as every library we install, ends up within the boundaries of the appropriate virtual environment. In our example, ProjectX will hold Django 1.7, while ProjectY will hold Django 1.8.

This sounds little bit similar to the concept of application virtualisation scenario. However, these are completely different platforms. 

Let us see through sample example here through screenshots.

First of all, we will install virtualenv tool. I am using macOS, hence I will be using terminal for doing so. For Debian based linux distribution, use below command to do the installation. 

$ sudo apt-get install python-virtualenv

For windows, refer virtualenv official website for installation steps.

Installing virtualenv:

1. start terminal and run pip3 install virtualenv on terminal

2. Verify that installation completed successfully.

3. That completes the virtualenv installation.

Creating Virtual Environment:

1. Create a directory for your projectX using mkdir on desired path and navigate to the folder on terminal using cd <folder path>.

2. After creating the virtual environment directory, we need to select the target python interpreter 
with project directory as you may have multiple versions.

4. Now activate the virtual environment with script /bin/activate. It is sourced which means it runs in current shell. The moment it is activated, your prompt will change as shown below.

5. You can also verify the target interpreter as shown below. It should be pointing to your project directory.

6. Launch python interactive shell to verify it is working fine

7. You can follow similar steps to create another project. Also if you want to deactivate project, you can simply run deactivate command on terminal as below.

NOTE: There is also another module called as venv which is suggested way of creating virtual environments as of Python 3.5

I hope this helps !!!!

No comments:

Post a comment


Popular Posts This Week