Virtual Environment in Python

Saijal Shakya
Incwell Bootcamp
Published in
2 min readMay 1, 2019

A virtual environment is a tool that helps us to keep dependencies required by different projects. It is done by creating isolated virtual environment for each projects.

Need of Virtual Environment

If we are doing two different project on Python, for instance a web app with Django 1.9 and another with Django 2.1. In such situations virtual environment can be really useful to maintain dependencies of both the projects.

Virtual environment can be used for every Python based project. There are no limits for creating virtual environments, as these are just directories with few scripts.

How to use Virtual Environment

First we need to install pip

sudo apt install python3-pip

Pip is a package installer for Python. It can be used to install packages and dependencies for Python. If you are familiar with node package manager, pip is similar to it.

Next, install virtual environment

sudo pip3 install virtualenv

We can create multiple virtual environment now. Create a directory and create virtual environment in it.

mkdir projectName
cd projectName
virtualenv venv

Now, you can find “venv” directory in your project. Here, “virtualenv” is the command to make a new Virtual Environment. “venv” is the name of the Virtual Environment. You can keep any name for virtualenv. Now, to activate your virtual environment,

source venv/bin/activate
Activated virtual environment

After activated it shows (venv)

About activated virtual environment and deactivating it.

which python
which pip

Run above command to view where python is and where pip is located in your current virtual environment

Deactivating virtual environment

deactivate

--

--