Can I automatically change my PYTHONPATH when activating/deactivating a virtualenv?

python, virtualenv

Solution

This django-users post is probably going to help you a lot. It suggests using virtualenvwrapper to wrap virtualenv, to use the add2virtualenv command. Using this, when the environment is active, you can just call:

add2virtualenv directory1 directory2 ...

to add the directories to your pythonpath for the current environment.

It handles autonomously the PATH changes on environment switches. No black magic required. Et voila!

Problem

I would like to have a different PYTHONPATH from my usual in a particular virtualenv. How do I set this up automatically? I realize that it's possible to hack the `bin/activate` file, is there a better/more standard way?

Original source