What is virtualenv
A Virtual Environment is a tool for python to keep the dependencies required by different projects isolated in separate places, by creating virtual Python environments.
It solves the problem “ProjectX depends on libraryA of version 1.0 but ProjectY needs library of version3.0” dilemma. Keeps the global site-packages directory clean and manageable.
Without virtualenv you would need to install/uninstall dependencies while you switch between projects that are having dependencies of same libraries but different versions.
For example, with virtualenv you can work on a project which requires Django 1.8 while also maintaining a project which requires Django 1.6.
The virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables and packages that a Python project would need.
Install virtualenv using pip:
$ pip install virtualenv
$ virtualenv --help
Usage: virtualenv [OPTIONS] DEST_DIR
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
--clear Clear out the non-root install and start from scratch.
--no-site-packages DEPRECATED. Retained only for backward compatibility.
Not having access to global site-packages is now the
Give the virtual environment access to the global
--always-copy Always copy files rather than symlinking.
--unzip-setuptools Unzip Setuptools when installing it.
--relocatable Make an EXISTING virtualenv environment relocatable.
This fixes up scripts and makes all .pth files
--no-setuptools Do not install setuptools (or pip) in the new
--no-pip Do not install pip in the new virtualenv.
--no-wheel Do not install wheel in the new virtualenv.
Directory to look for setuptools/pip distributions in.
This option can be used multiple times.
--never-download DEPRECATED. Retained only for backward compatibility.
This option has no effect. Virtualenv never downloads
pip or setuptools.
--prompt=PROMPT Provides an alternative prompt prefix for this
--setuptools DEPRECATED. Retained only for backward compatibility.
This option has no effect.
--distribute DEPRECATED. Retained only for backward compatibility.
This option has no effect.
How to use virtualenv
- Create a virtual environment for a project:
Shell12$ cd workspace$ virtualenv venv
Command virtuale venv will create a folder in the current directory with name venv and will put all necessary python libraries required to work virtual environment isolated. That is, which will contain the Python executable files, and also the
pip library where you can use to install other packages.
Here the name of the virtual environment is venv as we have given that name to virtualenv command. If you don’t specify a name or directory, current directory will be used to create virtual environment.
You can also specify a Python interpreter of your choice.
$ virtualenv -p /usr/bin/python2.7 venv
This command will tell python virtualenv to use the Python interpreter from location specified /usr/bin/python2.7
- To Start using the virtual environment, it needs to be activated:
$ source venv/bin/activate
To activate virtual environment, we use the command source with argument as a file activate which will reside in bin directory of created virtual environment directory. The name of the current virtual environment will now appear on the left of the prompt (something like (venv)User@HostName:/path/to/directory$ ) to let you know that it is activated. As long as this virtual environment activated, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.
You can Install packages as usual using pip:
$ pip install requests
- Once you are done working with virtual environment, you can deactivate it:
deactivate will be aviable as a bash command once you activate virtual environment. It can be used to deactivate(exit from) virtual environment.
So, this puts you back to the system’s default Python interpreter with all its installed libraries.
If you want to delete a virtual environment, just delete its folder. (here in this case, it would be rm -rf venv.)
Creating virtual environment with the option
--no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later. [This is the default behavior for
virtualenv 1.7 and later.]
In order to keep your environment consistent or reinstall virutal environment or distriubute as package, it’s a good idea to have a list of dependencies/requirements. You can “freeze” the current state of the environment packages. To do so, run
$ pip freeze > requirements.txt
This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using “pip list”. Later it will be easier for you or other developer to recreate or reuse environment and install dependencies.
$ pip install -r requirements.txt
This can help make sure consistency across installations, across deployments, and across developers.