Saturday 8 March 2014

Virtualenv with Virtualenvwrapper power

Work with python, you should know about virtualenv which enable us as python developer to build great thing with different setup.

For the newbie, a quick info is you can run your app with different python version and different package version without version conflict.

For example we have twitterclient application. It build on python2.7 and Django1.3 . Now i need to test it using Django1.4 . So how we do this ?

We not need to uninstall the Django1.3, but we use virtualenv, and use virtualenvwrapper on top of it. It makes developer life easier.

Lets dive in to the usage. First you have pip already installed, if not install it using :

# easy_install pip


Then you install the virtualenv and virtualenvwrapper using pip.

# pip install virtualenvwrapper


This will download also the virtualenv as the dependency.

First thing you must add in your bash environment. In you home directory open your .bashrc with your favorite editor and add :

export WORKON_HOME=$HOME/.virtualenvs

source /usr/local/bin/virtualenvwrapper.sh

For windows install, you add in your windows Environment Variables :

   WORKON_HOME=$USERPROFILE$\.virtualenvs
   PATH = C:\python2.7\Scripts\


The WORKON_HOME tells where the virtualenv will install. When you create a new virtualenv, it will put under WORKON_HOME you specified in the bashrc .
This also can work with zsh and csh.

And you can start using virtualenv. Lets make a new virtualenv :

super@devstack:$mkvirtualenv newbigthing

(newbigthing)super@devstack:$


The output will be like the above. To get out from the env, just type deactivate and you got your normal env.

If you have many environment for project, you can switch easily, just issue :

$ workon newbigthing

$ workon somebiggerthing


Inside the virtualenv you can install python package as usual using pip which is installed by default when the env created.

This is faster then using virtualenv alone, example when you need to go to a virtualenv you need to issue command like this :

$source /env/newbigthing/bin/activate


So long to type, with virtualenvwrapper, just issue workon yourproject

Happy coding with Virtualenvwrapper .

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More