Python Virtual Environment

A cooperatively isolated runtime environment that allows Python users and applications to install and upgrade Python distribution packages without interfering with the behaviour of other Python applications running on the same system.


Python Virtual Environment with example

Assume that we have 2 application of Python, and we have to run those at same workstation, Applications named

  • User Management

  • Item Management

Both application need single dependent library called Authentication, so it’s easy to install Authentication plugin to both of the application, it’s really strait forward, But the problem happen when both application need different version of Authentication library. So if we install the Authentication library globaly then may User Management will work but Item Management will not work, because it needed different version of library.

For solve this problem we need to separate the environment of both Application. Virtual Environment help use to solve this problem easily by separating the dependency.


Clarification of Python Version Conflict & Virtual Environment


Python Virtual Environment


Installation of Python virtualenv

  • Open the cmd or terminal & write the below command for install virtualenv

pip install virtualenv


Use of Python virtualenv

  • Create a directory where you want to store all of your project files

  • Open the cmd / terminal / bash on the directory.

  • Run the below command

# Syntax of the command
python -m venv <directory-name>

# We will create a virtual directory called venv, then the command will be
python -m venv .venv
  • Result : It will create a directory called .venv and store all of dependent files there.


Active the virtualenv on Python

For Windows cmd

.venv/Scripts/activate

For Unix based OS

source .venv/bin/activate


Deactivate the virtualenv on Python

# This command will deactivate the virtual environment
deactivate