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,
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With