This is a quick guide to knowing and using Python’s Virtualenv which is an awesome utility to manage multiple isolated Python environments.
Why virtualenv is needed?
Case 1: If we have a Python application A which uses version 1.0 of package X, but is incompatible with higher versions of it. On the other hand we have an application B which uses version 1.1 of package X, but is incompatible with lower versions of it. We want to use both the applications on the same machine.
Case 2: There is a package Y, which is released in beta version recently and we want to try it out, but don’t want to install it in the global site-packages directory as it might break something.
In above two cases, virtualenv is very helpful. In first case we can create two virtualenvs and then install version 1.0 of package X in one and version 1.1 of package X in another. Two virtualenvs would be isolated from each, hence we are not overwriting each other while installing different versions. In second case, we can create a virtualenv and install package Y in that, which would install Y in site-packages directory of the virtualenv instead of the global site-packages.
How to install virtualenv?
virtualenv can be installed directly using pip.
pip install virtualenv
How to use virtualenv?
First create a virtualenv using –
virtualenv env1
“env1″ is your environment name, change it accordingly. This would create a vritualenv named env1. Now wherever you run this command, it would create a directory env1 there. To activate and start using this newly created virtualenv, go to env1/bin and run –
source activate
Now you are inside the virtualenv. Any package that you install now, would get installed to env1/lib/pythonx.x/site-packages, instead of global site-packages. Any package that you install now doesn’t affect your global packages and other virtualenvs. To exit this virtualenv, run –
deactivate
To remove a virtualenv, simply delete the corresponding directory.
Share and Enjoy
• Facebook • Twitter • Delicious • LinkedIn • StumbleUpon • Add to favorites • Email • RSSThe post Quick Guide to Using Python’s Virtualenv appeared first on Harsh Tech Talk.