We use Python libraries such as NumPy, Pandas, Tensorflow, NLTK and many others when we develop Python applications. Usually, we install these libraries using the PIP package manager when we need them. But after that, we often forget about them, but still, they need some disk space. Also, sometimes we need to use different library versions for different applications. These kinds of problems can be easily solved by using Python Virtual Environments (virtualenv). Libraries installed in such an isolated virtual environment only related to that environment. You can create many virtual environments as much as you need and you can even remove them when you do not use them anymore.

Python Virtual Environment Prerequisites

First, check whether you have installed the PIP package manager in your system by running the following command. It should print the PIP version.

pip -V

If you get an error message something like “Command 'pip' not found”, you can install it by running the following commands.

sudo apt-get update
sudo apt-get install python3-pip

Python virtualenv Installation

Run the following command to check whether your system already has the virtualenv.

virtualenv --version

If it prints something like “virtualenv: command not found” instead of its version, you need to install the virtualenv by running the following command.

pip install virtualenv

Python Virtual Environment Creation

You can create a simple Python virtual environment by running the below command. It will create a directory called “venv” in the current directory with the necessary files to activate the virtual environment.

virtualenv venv

In addition to that, you can create a virtual environment with a specific Python interpreter version by using the -p flag. For example, if you have multiple versions of Python in your system, you can instruct virtualenv to use a specific interpreter like below.

virtualenv -p /usr/bin/python2.7 venv

Python Virtual Environment Activation

Once you created a virtual environment, it will not be activated immediately. Instead, you need to manually activate it like below. If you have used any other directory name instead of the “venv”, make sure to use it.

source venv/bin/activate

Once activated, you will be able to see the name of the virtual environment in the terminal.

Python Virtual Environment Usage

Now you can install libraries and run Python applications in this virtual environment as you used to be. For the demonstration, just install “NumPy” using the following command.

pip install numpy

Python Virtual Environment Deactivation

Once you finished your work, you can exit from the virtual environment by using the deactivate command.

deactivate

The name of the virtual environment will also be removed immediately as you deactivate the virtual environment. Once you deactivated the virtual environment, you can run the pip list to see that “NumPy” is not available anymore.

pip list

But if you activate the virtual environment again, the “NumPy” will be immediately available.

Python Virtual Environment Removal

If you do not use the virtual environment anymore, you can remove it. Make sure it is not activated and then run the following command to remove the “venv” directory.

rm -rf venv