Getting started with virtualenv
This section provides an overview of what virtualenv is, and why a developer might want to use it.
It should also mention any large subjects within virtualenv, and link out to the related topics. Since the Documentation for virtualenv is new, you may need to create initial versions of those related topics.
Installation or Setup
Virtual Environment tool (virtualenv) is used to isolate different projects and their dependencies by creating individual python environments for each of them. It's like installing a package locally (and not globally), similar to npm package installation option. Following is an example to install and test virtualenv for creating two projects (Project1-A Django application and Project2- A Flask application):
- Initially check if virtualenv is already installed
$ virtualenv --version
$ pip install virtualenv(for Mac and Linux) or
$ sudo apt-get install python-virtualenvfor Ubuntu,
easy_installfor Windows to install the python environment.
$ mkdir Project1and
$ cd Project1
$ virtualenv venvp1and this would create a venvp1 folder inside Project1 directory.
- To activate the environment run
source venvp1/bin/activate(if Linux) and
venvp1\Scripts\activate(if Windows) and prompt will change to
pip install Djangoto install Django for project1 and
deactivate(if needed) to return to the global environment.
- Repeat steps 3-6 for Flask application with different directory, virtualenv names and
pip install Flaskto install Flask.
Once above steps are executed (without any errors) one could (possibly and) simultaneously work between both environments without any conflicts.
- virtualenvwrapper is another handy tool which is extended version of virtualenv, though the installation procedure for both is nearly same.
--no-site-packagesexcludes the globally installed packages.
- To freeze current state of environment run
$ pip freeze > installedpkgp1.txt. This text file contains list of installed packages (including their versions) in the current environment. If there comes a need to deploy same environment at different folder (or machine) simply executing the command
$ pip install -r installedpkgp1.txtwould create same environment.
- Useful commands:
lsvirtualenv- list of all environments
cdvirtualenv- goto currently activated virtual environment
cdsitepackages- like previous, but goes directly to
lssitepackages- shows content of