In a simplified form, the activate
will modify the environment variables during the terminal session.
[...]
VIRTUAL_ENV="/home/user/python/venv"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
[...]
Code snippet from archive venv/bin/activate
That is, when you activate the virtual environment an environment variable VIRTUAL_ENV
is created with the path to where the environment was generated; soon after it modifies the value of the variable PATH
adding the newly created variable as the first path in the PATH search.
This way, when you run some command with the virtual environment enabled, the first directory to be analyzed in search of the executable of this command will be the directory venv/bin
(in Windows will be venv/Scripts
).
When performing the function deactivate
the variables will return to the initial value, that is, the variable VIRTUAL_ENV
shall be excluded and the PATH
will return to value _OLD_VIRTUAL_PATH
, created during the activate
with the original value of PATH
.
When you log out of your terminal, these environment variables will be removed, so there is no obligation for you to always call deactivate
. You need to run it only when you need to quit the virtual environment while maintaining the same terminal session - common when you are, for example, connected via SSH to the server.
Navigating between folders during the same terminal session will not cause your virtual environment to be disabled.
Jeferson, the question is about
virtualenv
, and your answer talks aboutvirtualwrapper
. From what I’ve researched, neither the modulevenv
of Python3 and the libraryvirtualenv
have the commandworkon
added to the PATH.– fernandosavio
virtualenv is a creator of virtual environments, virtualwrapper manages. Using virtualenv only will have to enable and disable. Both are manually initialized. However if you always use a version you can specify. The examples are just a mention of the act of having to activate and deactivate. deactivate or even list lsvirtualenv. The part I explained is equivalent, the example was to enable and disable.
– stack.cardoso