This is my first approach to direnv. I've been trying to adapt direnv to Poetry and venv.
~/.zshrc:
# skip loading direnv if it is already loaded
if test -z $DIRENV_DIR; then
if whereis direnv 2&>1 1> /dev/null ; then
case "$(basename $SHELL)" in
zsh)
eval "$(direnv hook zsh)"
;;
bash)
eval "$(direnv hook bash)"
;;
*)
echo "skip" /dev/null
;;
esac
fi
fi
# substitute prompt (direnv)
setopt PROMPT_SUBST
show_virtual_env() {
if [[ -n "$VIRTUAL_ENV" && -n "$DIRENV_DIR" ]]; then
echo "($(basename $VIRTUAL_ENV)) "
fi
}
PS1='$(show_virtual_env)'$PS1
~/.config/direnv/direnvrc:
layout_poetry() {
PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
if [[ ! -f "$PYPROJECT_TOML" ]]; then
log_status "No pyproject.toml found. Executing \`poetry init\` to create a \`$PYPROJECT_TOML\` first."
poetry init
fi
if [[ -d ".venv" ]]; then
VIRTUAL_ENV="$(pwd)/.venv"
else
VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true)
fi
if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
log_status "No virtual environment exists. Executing \`poetry install\` to create one."
poetry install
VIRTUAL_ENV=$(poetry env info --path)
fi
PATH_add "$VIRTUAL_ENV/bin"
export POETRY_ACTIVE=1
export VIRTUAL_ENV
}
Everything works, but there's no way to deactivate the virtual environment.
.envrc with Poetry:
layout poetry
.envrc with (python -m venv .venv):
layout python
In both cases, C-d or exit exits the shell, and deactivate is unavailable, which is somewhat unexpected in the latter case. What can I do to deactivate the virtual environment and stay within the same directory? And to get back into the virtual environment the same way, for that matter.