You might also try using the VIRTUAL_ENV environment variable as well. I found this to be a clearer solution after viewing this, so I thought I'd leave a note in case this is reaching others searching for similar solutions.
E.g.,
if [ ${VIRTUAL_ENV} ]
then
# do some stuff
fi
Here's the relevant portion of the deactivate
function in the activate
script:
# (inside deactivate function)
unset VIRTUAL_ENV
if [ ! "$1" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
Here's how that gets set when you originally source the file:
# (runs when you source the activate file)
VIRTUAL_ENV="/path/to/venv/dir"
export VIRTUAL_ENV
This may not solve the original question (didn't test), but it's helpful for a large subset of cases where you just need to know if you're in a virtualenv or not.