In this article, we will see what are virtual environments, how to activate and deactivate them and what are their benefits?


Note: This tutorial is mainly for windows users.

1. What are virtual environments?

Virtual environments are basically containers, which helps to isolate different python projects from each other. Which means that, each virtual environment can have its own dependencies, regardless of the other virtual environments. Any change in one virtual environment won’t have any affect in other virtual environments or even the system-wide libraries. Thus, we can create as many virtual environments as we want because they are just directories or folders containing few scripts.

2. Why are python virtual environments important ?

The importance of python’s virtual environments becomes apparent when we have different python projects on the same machine that depends on different versions on the same package. Since python cannot simultaneously use multiple versions of the same package, this would result in Compatibility Issues.

Another reason to use virtual environments, when you have a python project developed and deployed, and you can’t modify packages because of the deployment requirements.

3. How to use Virtual Environments ?

To get started with using virtual environments, you will first need to import it using pip.

pip install virtualenv

Create a new directory

mkdir python-virtual-environment && cd python-virtual-environment

mkdir (make directory) creates a new folder and cd (change directory) allows you to enter in to this newly created directory. Once we are inside this directory, we can run :

virtualenv environment

After writing above and pressing enter, it will create a new virtual environment, named as “environment“.

virtual-environment-windows
virtual environment created using virtualenv

As we can see from the above image, that using virtualenv, we can easily create virtual environments.

4. How to activate and deactivate a virtual environment?

Activating and deactivating virtual environments is as easy as creating virtual environments.

Activate

To activate, all you need to do write your virtual environment name, then \Scripts\activate and press enter:

environment\Scripts\activate
activating-a-virtual-environment
Activating a virtual environment

We can confirm that our virtual environment is activated, if we can see our virtual environment name, before the file path.

Deactivate

Similarly, to deactivate, we can write environment name\Scripts\deactivate and press enter :

environment\Scripts\deactivate
deactivating-a-virtual-environment
deactivating a virtual environment

We can confirm that our virtual environment is deactivated, if our virtual environment name is not shown before the file path.

5. Conclusion

In this article, we have learned about virtual environments, what are these, how do we use them. How can we activate or deactivate a virtual environment, and how it helps us to isolate our python projects, so each project can have its own dependencies.

More Articles :

Basics of Javascript

Leave a Reply

Your email address will not be published.