Search This Blog

Friday, April 19, 2013

Working with multiple Python projects

While working with multiple Python projects, you might soon realize you need different versions of the same Py package or packages are conflicting between multiple projects.
VirtualEnv to your rescue.
But once you go through the docs, install and start using virtualenv, you might soon realize it is a little cumbersome.
  1. You need to provide the complete path where you want to save your virtualenv environment folder. virtualenv proj1 will just create the env in your current directory. If you are using a long path to store your envs, it might be a pain to put it in every-time.
  2. To activate an env, source <path-to-envs>/proj1/bin/activate
  3. To find all envs, you have to ls <path-to-envs>. But this works only if you have all yours envs in a single place.
Don't worry, VirtualEnvWrapper is here to help you.
Now you have a wrapper which provides you some shortcuts to the virtualenv commands. But it doesn't end there. VirtualEnvWrapper provides you with the an extension system.
ls $WORKON_HOME and you will find files starting with "post" and "pre". These are simple shell scripts. You can add your commands to these scripts and they will be everytime you run one of the commands.
It doesn't end there. You can also have separate extensions for each of your env. ls $WORKON_HOME/proj1/bin/ should show some similar files starting with "post" and "pre". For eg. To go to your source folder when you activate your env, just add "cd to "postactivate"
All said and done, one thing that might trouble you is figure out what packages are available in your current env?
Yolk will prove useful here. Remember, you can write extensions in wrapper. Just add pip install yolk to "$WORKON_HOME/postmkvirtualenv" and yolk will be installed on all new virtual envs created automatically. You can then activate your env and just use yolk -l to list packages available in that env.
Not much time for formatting. Will do it later.

No comments: