Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtualenv and VIRTUAL_ENV keyword

After having installed a new virtualenv, for example called ENV, if I type

. /path/to/ENV/bin/activate

python
import os
print os.environ['VIRTUAL_ENV']

Then I see the /path/to/ENV/

However, if I type

/path/to/ENV/bin/python

And then

import os
print os.environ['VIRTUAL_ENV']

I've got a key error So what is the fundamental difference between these two methods? Thanks,

like image 668
nam Avatar asked Feb 06 '13 16:02

nam


People also ask

What is activate virtualenv?

Activate the virtual environment Activation makes the virtual environment the default Python interpreter for the duration of a shell session. You'll need to use different syntax for activating the virtual environment depending on which operating system and command shell you're using.

How do I know if my virtual environment is activated?

Check the $VIRTUAL_ENV environment variable. The $VIRTUAL_ENV environment variable contains the virtual environment's directory when in an active virtual environment. Once you run deactivate / leave the virtual environment, the $VIRTUAL_ENV variable will be cleared/empty.


1 Answers

Inside the script at bin/activate, there's a line that looks like this:

VIRTUAL_ENV="/Users/me/.envs/myenv"
export VIRTUAL_ENV

Which is what's responsible for setting your VIRTUAL_ENV environment variable. When you don't use activate, that variable never gets exported - so it's not present in os.environ.

like image 92
girasquid Avatar answered Sep 21 '22 22:09

girasquid