Virtual environment¶
A virtual environment is a “space” separated from operating system, where Python packages get installed. This “space” is local, thus prevents Python packages from overwriting your system Python packages.
Imagine having package xyz in version 1.2.4 installed system-wide. Now
you want to install Ganeti Web Manager, that requires this package in version 1.1.
You can’t keep both packages installed system-wide. Therefore you need to somehow separate packages required by Ganeti Web Manager and your system packages. Virtual environment does exactly this.
Virtual environment structure¶
Virtual environment (shortly virtualenv or venv) consists of these
directories:
bin- contains executable files and activation scriptsinclude- contains symlink tolib/python2.xdirectorylib- Python packages get installed to this directorylocal- contains symlinks tobin,includeandlibdirectoriesshare- contains documents andmanpages installed along with Python packages
Helpers and tools¶
Main tool used for creating virtual environments is python-virtualenv and
it’s executable: virtualenv.
When you issue virtualenv name in your shell, this tool creates structure
described above in the name directory.
Usually next thing to do when developing (or deploying) a project in Python is to clone a repository within that virtual environment. It creates your project files next to virtual environment’s directories. And everything becomes a mess.
To help overcome this mess, someone clever wrote virtualenvwrapper. This
is a set of shell scripts, that:
create virtual environment in your
$HOME/.virtualenvsdirectorylist virtual environments existing there
remove specified virtual environment
quickly switch between existing virtual environments
…and we highly recommend using it.
virtualenvwrapper commands¶
mkvirtualenv nameCreates virtual environment with given name.
lsvirtualenvList all existing virtual environments.
rmvirtualenv nameRemove existing virtual environment with given name.
workon nameSwitch to virtual environment with given name.
deactivateWhen you’re within virtual environment, you can leave it by issuing this command.
Command line prompt¶
By default, after activating specific virtual environment, it’s name appears at the beginning of your shell prompt. For example:
$ cd ganeti_webmgr
$ workon gwm
(gwm)$ django-admin.py --help
...
(gwm)$ deactivate
$ lsvirtualenv
Note
In some guides in this documentation these brackets indicate commands issued from within virtual environment.