Switch virtualenvs as easy as changing directories!
detect_virtualenv
is a function that will search for virtualenvs in the current directory and its parents. When it finds one, it will activate it; if it doesn't find one, it will deactivate any active virtualenv.
By default, it will only detect a virtualenv called .venv
. To instead detect virtualenvs with other names, set the MY_VIRTUALENV_NAMES
variable to a colon-separated list of the names you use e.g. export MY_VIRTUALENV_NAMES=cool_virtualev:rad_venv:ve
.
git clone https://github.com/dgholz/detect_virtualenv.git ~/.vendor/detect_virtualenv
. ~/.vendor/detect_virtualenv/detect_virtualenv # enable just for this sesson
echo . ~/.vendor/detect_virtualenv/detect_virtualenv >> ~/.bashrc # enable for new sessions
export MY_VIRTUALENV_NAMES=cool_virtualev:rad_venv:ve # add this to your ~/.bashrc to set it for all new sessions
pushd a/dir/with/a/virtualenv
detect_virtualenv # finds and activates a virtualenv called virtualenv_run or venv
cd subdir
detect_virtualenv # finds the virtualenv in the parent
popd
detect_virtualenv # deactivates the previously-found virtualenv
detect_virtualenv
accepts some command-line options:
--off
disables detection of virtualenvs when callingdetect_virtualenv
--deactivate
same asoff
and also deactivates the current virtualenv--on
enables detection of virtualenvs when callingdetect_virtualenv
(on by default)--status
prints whether detection of virtualenv is enabled ('on') or disabled ('off')--toggle
enables detection of virtualenv if currently disabled, and vice versa<name>[:<name>] ...
a colon-separated list of virtualenvs to detect in place of the current value of$MY_VIRTUALENV_NAMES
You can configure your shell to automatically execute detect_virtualenv
.
detect_virtualenv
provides a helper function, which is automatically run if it is sourced with on_prompt
as the first argument:
. path/to/detect_virtualenv on_prompt
It will avoid adding to PROMPT_COMMAND
if it is already present.
Not recommended, as other tools may override cd
et al. e.g. rvm
. Only use if you are certain no other part of your ~/.bashrc
changes these builtins:
. path/to/detect_virtualenv
function cd() { builtin cd "$@" && detect_virtualenv }
function pushd() { builtin pushd "$@" && detect_virtualenv }
function popd() { builtin popd "$@" && detect_virtualenv }