Getting started with virtualenv
Remarks#
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
- Run
$ pip install virtualenv
(for Mac and Linux) or$ sudo apt-get install python-virtualenv
for Ubuntu,easy_install
for Windows to install the python environment. $ mkdir Project1
and$ cd Project1
- Run
$ virtualenv venvp1
and this would create a venvp1 folder inside Project1 directory. - To activate the environment run
source venvp1/bin/activate
(if Linux) andvenvp1\Scripts\activate
(if Windows) and prompt will change to(venvp1)Your-Computer:your_project UserName$)
- Run
pip install Django
to install Django for project1 anddeactivate
(if needed) to return to the global environment. - Repeat steps 3-6 for Flask application with different directory, virtualenv names and
pip install Flask
to install Flask.
Once above steps are executed (without any errors) one could (possibly and) simultaneously work between both environments without any conflicts.
Notes:
- virtualenvwrapper is another handy tool which is extended version of virtualenv, though the installation procedure for both is nearly same.
- Executing
virtualenv
command with--no-site-packages
excludes 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.txt
would create same environment. - Useful commands:
lsvirtualenv
- list of all environmentscdvirtualenv
- goto currently activated virtual environmentcdsitepackages
- like previous, but goes directly tosite-packages
directorylssitepackages
- shows content ofsite-packages
directory