Python virtualenv primer

virtualenv is a Python package which enables the creation of isolated environments for projects. This allows different version of packages (Djano, for example) to co-exist on the same system and be used for different projects. This means cleaner systems (the only Python package which needs to be installed globally is virtualenv), easier application packaging and testing through simply copying a virtualenv environment.

Install or upgrade virtualenv

pip istall virtualenv
pip install virtualenv --upgrade

Creating a virtualenv

To create a new environment, run the following command

# virtualenv --no-site-packages cdb

New python executable in cdb/bin/python Installing setuptools, pip…done.

The --no-site-packages option instructs virtualenv to not symlink packages installed the global site-packages to the virtualenv if that same package is installed globally.

Using a virtualenv

Once the virtualenv has been created, we need to activate it in order to use it. Activation updates the $PATH environment variable to the virtualenv.

[root@srv-testlab1 virtualenvs]# cd cdb/
[root@srv-testlab1 cdb]# source bin/activate
(cdb)[root@srv-testlab1 cdb]#

Once activated, the virtualenv name will be prepended to the prompt. To deactivate, or ‘log out’ of an environment, run deactivate

From here we can install required packages using pip and begin working on our project.


If you enjoyed this post consider sharing it on , , , or , and .