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):
$ virtualenv --version
$ 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
$ virtualenv venvp1
and this would create a venvp1 folder inside Project1 directory.source venvp1/bin/activate
(if Linux) and venvp1\Scripts\activate
(if Windows) and prompt will change to (venvp1)Your-Computer:your_project UserName$)
pip install Django
to install Django for project1 and deactivate
(if needed) to return to the global environment.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:
virtualenv
command with --no-site-packages
excludes the globally installed packages.$ 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.lsvirtualenv
- list of all environmentscdvirtualenv
- goto currently activated virtual environmentcdsitepackages
- like previous, but goes directly to site-packages
directorylssitepackages
- shows content of site-packages
directory