OverIQ.com

How to use Virtualenv?

Last updated on July 27, 2020


What is Virtualenv? #

Virtualenv is a handy tool for creating Python Virtual Environments.

So Why do we need Virtual Environments?

You can think of Virtual Environments as a separate Python installation which allows us to work on projects using different versions of the same package without conflicting with one another. Consider the following example:

Let's say we are working on an e-commerce website and a CRM. Our e-commerce website depends upon version 1 of the foo package but our CRM needs version 2. At a time, we can only work with one version of the foo package. We can't have both versions simultaneously on the system. A virtual environment can resolve these kinds of problem easily.

Another excellent use case of Virtualenv is that when you are working on a system and you don't have the privilege to install packages globally. What you can do is create a virtual environment in your home directory and then install all the dependencies of the project inside the virtual environment.

Even if you don't work with conflicting packages and have full access to the system, it is still a good idea to always start your new project in a virtual environment that way if anything goes wrong with the project, your system-wide Python installation will remain intact.

Now you the Why of Virtualenv let's get into the How?

Installing Virtual #

To install Virtualenv type the following command:

pip3 install virtualenv

Creating Virtual Environment #

We now ready to create virtual environments. Create a new directory my-project and change your current working directory to this directory using the cd command:

1
2
$ mkdir my-project
$ cd my-project

To create the virtual environment type the following:

$ virtualenv env

Alternatively, you can also use the following command:

$ python -m virtualenv env

This will create a new a new directory named env in your current working directory. The structure of the env directory should look like this:

1
2
3
4
5
env/
├── bin/
├── include/
├── lib/     
└── pip-selfcheck.json

So what are these files and folder?

These files and folder constitute a separate Python installation.

The bin/ folder contains all the executables that you would find in an ordinary Python installation:

1
2
3
4
$ ls env/bin/
activate       activate_this.py  pip     python     python-config
activate.csh   easy_install      pip3    python3    wheel
activate.fish  easy_install-3.5  pip3.5  python3.5

By default, Virtualenv creates virtual environment using the version of Python under which it is installed. In other words, if Virtualenv is installed as a package of Python 3.5 then it will create virtual environment using Python 3.5. We can specify another version of the Python using the -p option.

virtualenv env -p /usr/bin/python2

This will create a virtual environment using Python 2.7 instead of Python 3.5.

Activating the Virtual Environment #

To use a virtual environment, we first have to activate it. Activate virtual environment by typing the following command:

1
2
$ source env/bin/activate
(env) $:

If you are on Windows use the following command:

1
2
C:\Users\user>env\bin\activate
(env) C:\Users\user>

Our virtual environment is now active. Did you notice (env) in front of the shell prompt? It indicates that the virtual environment named env is up and running.

Activating a virtual environment changes the $PATH environment variable temporarily so that the bin/ directory of the virtual environment will become first in the list. If you now execute python command, Python executable which resides in the env/bin/ directory is executed instead of the globally installed Python.

Once virtual environment is activated, any package you add or remove using the pip will only affect the virtual environment you are working on. Packages installed at system-wide installation will not be affected at all.

Deactivating the Virtual Environment #

Once you are done working with the Virtualenv you can deactivate it using the deactivate command:

$ deactivate

This will remove the bin/ directory from the $PATH environment variable, making globally installed Python accessible again.

Inheriting Packages using --system-site-packages #

On some occasions, you might want to create a virtual environment with the packages from the global Python installation. This can be accomplished using --system-site-packages option.

$ virtualenv env --system-site-packages